This directory contains examples showing how to use MoroJS across different runtime environments while maintaining the same API and developer experience.
MoroJS now supports multiple runtime environments through a unified interface:
- Node.js (default) - Traditional Node.js HTTP server
- Vercel Edge - Vercel Edge Functions
- AWS Lambda - AWS Lambda functions with API Gateway
- Cloudflare Workers - Cloudflare Workers
✅ Same API everywhere - Your route definitions work identically across all runtimes ✅ Zero breaking changes - Existing Node.js code continues to work unchanged ✅ Runtime-specific optimizations - Each adapter is optimized for its environment ✅ Type safety - Full TypeScript support for all runtime environments ✅ Automatic request/response adaptation - Seamless conversion between runtime formats
import { createApp } from '@morojs/moro';
const app = await createApp();
app.get('/', (req, res) => {
return { message: 'Hello World!' };
});
app.listen(3000);import { createAppEdge } from '@morojs/moro';
const app = await createAppEdge();
app.get('/', (req, res) => {
return { message: 'Hello from the Edge!' };
});
export default app.getHandler(); // Export for Vercelimport { createAppLambda } from '@morojs/moro';
const app = await createAppLambda();
app.get('/', (req, res) => {
return { message: 'Hello from Lambda!' };
});
export const handler = app.getHandler(); // Export for Lambdaimport { createAppWorker } from '@morojs/moro';
const app = await createAppWorker();
app.get('/', (req, res) => {
return { message: 'Hello from Workers!' };
});
export default {
async fetch(request, env, ctx) {
return app.getHandler()(request, env, ctx);
},
};- Full HTTP server capabilities
- WebSocket support
- File system access
- Process management
- Global edge deployment
- Fast cold starts
- Streaming responses
- Geographic routing
- Auto-scaling
- Pay-per-request
- VPC integration
- Event-driven architecture
- Global edge network
- Instant deployment
- KV storage integration
- Durable Objects support
You can also use the generic createApp() with runtime configuration:
import { createApp } from '@morojs/moro';
const app = await createApp({
runtime: {
type: 'vercel-edge', // 'node' | 'vercel-edge' | 'aws-lambda' | 'cloudflare-workers'
options: {
// Runtime-specific options
},
},
});-
Change the import:
// From import { createApp } from '@morojs/moro'; // To (for example, Vercel Edge) import { createAppEdge } from '@morojs/moro';
-
Replace
listen()withgetHandler():// From app.listen(3000); // To export default app.getHandler();
-
Update deployment configuration (see individual examples)
-
Test runtime-specific features (optional)
- File system access: Limited in edge/serverless environments
- WebSockets: Not available in Lambda/Edge (use alternative solutions)
- Long-running processes: Not suitable for serverless environments
- State persistence: Use external storage in serverless environments
- Keep handlers stateless - Don't rely on global state
- Use environment variables - For configuration across runtimes
- Optimize for cold starts - Minimize initialization code
- Handle errors gracefully - Each runtime has different error handling
- Test locally - Use runtime-specific development tools
- Develop locally with Node.js runtime
- Test with target runtime using local simulators
- Deploy to staging environment
- Monitor performance and adjust as needed
node-example.ts- Traditional Node.js serververcel-edge-example.ts- Vercel Edge Functionsaws-lambda-example.ts- AWS Lambda with API Gatewaycloudflare-worker-example.ts- Cloudflare Workers
Each example includes deployment instructions and runtime-specific configurations.