This topic describes the request and response specifications of HTTP functions for custom container runtime and provides sample code.

Background

Function Compute forwards user requests, including the path, request headers, request body, and common headers, to the HTTP server. Unlike with event functions, you do not need to implement /invoke and /initialize. HTTP functions enable you to smoothly migrate an existing HTTP web application. For more information, see Function calls in a custom runtime.

Input parameters of functions

  • event: the body of the POST request
  • context:
    • The x-fc-access-key-id, x-fc-access-key-secret, and x-fc-security-token headers are used to obtain temporary credentials for the service role to access other Alibaba Cloud services.
    • The x-fc-request-id header is used to obtain the current request ID.
    • For more information about request headers, see Common headers.

Sample code

In the following Node.js Express example, the GET and POST methods are routed to different handlers. You can map a path to a handler that you need.

'use strict';

const express = require('express');

// Constants
const PORT = 9000;
const HOST = '0.0.0.0';

// HTTP function get
const app = express();
app.get('/*', (req, res) => {
  res.send('Hello FunctionCompute, http GET');
});

const app = express();
app.post('/*', (req, res) => {
  res.send('Hello FunctionCompute, http POST');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);