Node Functions on EdgeOne Pages - Koa

Node Functions allow you to run code in a Node Runtime without managing servers. With its capabilities, you can easily develop and deploy full-stack applications based on the Koa framework on EdgeOne Pages.

./node-functions/koa/[[default]].js

import Koa from 'koa';
import Router from '@koa/router';

// Create Koa application
const app = new Koa();
const router = new Router();

// Add some middleware
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
});

// Define routes
router.get('/', async (ctx) => {
  ctx.body = { message: 'Hello from Koa on Node Functions!' };
});


// Use router middleware
app.use(router.routes());
app.use(router.allowedMethods());

// Export the handler
export default app;