Runtime Framework

Modern.js BFF supports different runtime frameworks. Currently, Modern.js BFF supports two runtime frameworks: Express.js and Koa.js.

When using different runtime frameworks, some APIs may differ.

Accessing Request Context

In BFF functions, you may need to access the request context to handle more logic. Depending on the runtime framework, you need to use different APIs:

Express.js
Koa.js
api/lambda/hello.ts
import { useContext } from '@modern-js/runtime/express'
export const get = async () => {
  const { req } = useContext();
  console.info(`access url: ${req.url}`);
  return 'Hello Modern.js'
};
INFO

For more details, refer to useContext.

Using Middleware

In BFF functions, you may need to use middleware to handle more logic. Depending on the runtime framework, you need to use different APIs:

Express.js
Koa.js
api/_app.ts
import { hook } from '@modern-js/runtime/express';

export default hook(({ addMiddleware }) => {
  addMiddleware((req, res, next) => {
    req.query.id = 'koa';
    next();
  });
});
INFO

For more details, refer to hook.