Advanced
Custom Adapters
MoroJS includes built-in runtime adapters for different deployment targets. Learn about available adapters and how they enable multi-platform deployment.
Available Runtime Adapters
Node.js
Default runtime with full feature support
AWS Lambda
Serverless deployment with createAppLambda()
Cloudflare Workers
Edge deployment with createAppWorker()
Runtime Adapter Usagetypescript
1// Node.js (default)
2import { createApp } from '@morojs/moro';
3
4const app = await createApp({
5 cors: true,
6 compression: true,
7 helmet: true
8});
9
10app.get('/', (req, res) => {
11 return { message: 'Hello from Node.js!', runtime: 'node' };
12});
13
14app.listen(3000);
15
16// AWS Lambda
17import { createAppLambda } from '@morojs/moro';
18
19const app = await createAppLambda();
20
21app.get('/', (req, res) => {
22 return {
23 message: 'Hello from AWS Lambda!',
24 runtime: 'aws-lambda',
25 requestId: req.requestId
26 };
27});
28
29export const handler = app.getHandler();
30
31// Cloudflare Workers
32import { createAppWorker } from '@morojs/moro';
33
34const app = await createAppWorker();
35
36app.get('/', (req, res) => {
37 return {
38 message: 'Hello from Cloudflare Workers!',
39 runtime: 'cloudflare-workers',
40 edge: true
41 };
42});
43
44export default {
45 async fetch(request, env, ctx) {
46 return app.getHandler()(request, env, ctx);
47 }
48};Runtime Adapter Examples
AWS Lambda Adapter (Actual Implementation)typescript
1// AWS Lambda Runtime Example
2import { createAppLambda } from '@morojs/moro';
3import type { LambdaEvent, LambdaContext } from '@morojs/moro';
4
5const app = await createAppLambda();
6
7// Define routes exactly the same way as Node.js
8app.get('/', (req, res) => {
9 return {
10 message: 'Hello from MoroJS on AWS Lambda!',
11 runtime: 'aws-lambda',
12 timestamp: new Date().toISOString(),
13 requestId: req.requestId
14 };
15});
16
17app.get('/health', (req, res) => {
18 return {
19 status: 'healthy',
20 runtime: 'aws-lambda',
21 lambda: true,
22 region: process.env.AWS_REGION
23 };
24});
25
26app.post('/api/data', (req, res) => {
27 return {
28 received: req.body,
29 runtime: 'aws-lambda',
30 method: req.method,
31 pathParameters: req.params
32 };
33});
34
35// Export the handler for AWS Lambda
36export const handler = app.getHandler();
37
38// For typed Lambda handler (optional)
39export const typedHandler = (event: LambdaEvent, context: LambdaContext) => {
40 return app.getHandler()(event, context);
41};Cloudflare Workers Adapter (Actual Implementation)typescript
1// Cloudflare Workers Runtime Example
2import { createAppWorker } from '@morojs/moro';
3import type { WorkersEnv, WorkersContext } from '@morojs/moro';
4
5const app = await createAppWorker();
6
7// Define routes exactly the same way
8app.get('/', (req, res) => {
9 return {
10 message: 'Hello from MoroJS on Cloudflare Workers!',
11 runtime: 'cloudflare-workers',
12 timestamp: new Date().toISOString(),
13 cf: req.headers['cf-ray'] ? {
14 ray: req.headers['cf-ray'],
15 country: req.headers['cf-ipcountry']
16 } : null
17 };
18});
19
20app.get('/api/user/:id', (req, res) => {
21 return {
22 userId: req.params.id,
23 runtime: 'cloudflare-workers',
24 query: req.query,
25 location: {
26 country: req.headers['cf-ipcountry'],
27 region: req.headers['cf-region'],
28 city: req.headers['cf-city']
29 }
30 };
31});
32
33// Cloudflare Workers with environment variables
34app.get('/api/env', (req, res) => {
35 return {
36 hasEnv: !!(req as any).env,
37 runtime: 'cloudflare-workers',
38 envKeys: (req as any).env ? Object.keys((req as any).env) : []
39 };
40});
41
42// Export the handler for Cloudflare Workers
43export default {
44 async fetch(request: Request, env: WorkersEnv, ctx: WorkersContext) {
45 return app.getHandler()(request, env, ctx);
46 }
47};