Aller au contenu principal
Version: 3.0.0

Function Context

Event conversion

Midway Serverless has carried out input parameter wrapping according to the situation of different platforms. At the same time, when the function uses apigw(API gateway) and http (Aliyun) triggers, it has made special treatment on the input parameter (event). In order to simplify and unify the writing method, the event is unified and regularized into code similar to koa writing method.

Normal trigger scenario:

import { Context } from '@midwayjs/faas';
import { Provide } from '@midwayjs/core';

export class Index {

ctx: Context;

async handler(event) {
return 'hello world';

HTTP and API gateway trigger scenarios:

import { Context } from '@midwayjs/faas';
import { Provide } from '@midwayjs/core';

export class Index {

ctx: Context;

async handler() {
// The following two writing methods are the same
// this.ctx.body = 'hello world';
return 'hello world';


Every time a function is called, a new ctx (function context) is created. For attributes or methods on ctx, we provide ts definitions.


In Serverless v1 era, our definition is called FaaSContext. In v2, we have unified the definition and application, which is more consistent.


  • return ILogger

The log object of each request passed by the runtime. The default value is console.'hello');


  • return string

The current startup environment, that is, the value of the NODE_ENV or MIDWAY_SERVER_ENV. The default value is prod.

ctx.env; //default prod


  • return MidwayRequestContainer

The IoC request scope container of midway faas is used to obtain object instances in other IoC containers.

const userService = await ctx.requestContext.getAsync(UserService);


Context definitions are inherited from FaaSHTTPContext. The former retains the latter. In most scenarios, the former can be used directly. The latter is only available under apigw(API Gateway) and http (Aliyun) triggers.

For ordinary users, just use Context definition directly.

import { Context } from '@midwayjs/faas';

ctx: Context;

In the ctx object, we provide some API similar to writing traditional Koa Web applications. The advantage of this is to reduce the cognitive cost of users, and, to a certain extent, compatibility with the original traditional code and community middleware becomes possible.

We have provided some APIs similar to traditional APIs that support common capabilities. Different platforms may not be exactly the same. We will point out specific APIs.


  • return FaaSHTTPRequest

HTTP Request object simulated by FaaS.


  • return FaaSHTTPResponse

HTTP Response object simulated by FaaS.


The proxy is request.pathParameters and is available under http triggers (Aliyun) and API gateway triggers.

// /api/user/[id] /api/user/faas; // faas


Set the response header, which is the response.setHeader agent.

ctx.set('X-FaaS-Duration', 2100);


Sets the return status code, which represents the response.statusCode from.

ctx.status = 404;

Request aliases

The attributes listed below are from the [Request](# k6AZp) object proxy

  • ctx.headers
  • ctx.method
  • ctx.url
  • ctx.path
  • ctx.ip
  • ctx.query
  • ctx.get()

Response aliases

The attributes listed below are from the Response object proxy

  • ctx.body=
  • ctx.status= alias to response.statusCode
  • ctx.type=
  • ctx.set() alias to response.setHeader


This object is obtained by converting the event and context input parameters of the function.


Object containing all request headers, key-value pair storage.


obtain the client request ip address.


On Alibaba Cloud FC, only the HTTP trigger can obtain the value, and the api gateway cannot obtain the value for the time being.


the client requests the complete url.


the client request path.


The requested method.


The body of the POST request has been parsed to JSON.


This object is obtained by converting the event and context input parameters of the function.


Set the response header.


Set the return status code.


Set the content of the response body, string or buffer.