All Products
Search
Document Center

Function Compute:Event handlers

Last Updated:Feb 10, 2025

This topic covers the structure, sample code, and FAQ of event handlers in a Custom Container runtime.

Background

For a Custom Container function Function Compute forwards the common request headers, request body, POST method, and the /invoke and /initialize paths to the HTTP server in the container. You can use function signatures, such as context and event, from runtimes supported by Function Compute, such as a Go runtime. You can also use the request headers and body as input parameters to define the service logic of a function. For more information, see Event handlers in a custom runtime.

Function invocation

If a Custom Container function uses an event handler, the HTTP server needs to implement logic only for the /invoke path and the POST method.

Path

Request

Expected response

POST /invoke

  • Request body: the function input, which is the payload that you specify when you call the InvokeFunction operation.

  • Request header: common request headers. For more information, see Common request headers in Function Compute.

    Important

    Content-Type must be application/octet-stream.

Response body: the return value of the function handler, which includes the response code and response headers.

  • HTTP status code (StatusCode)
    • 200: succeeded
    • 404: failed
  • Response header (x-fc-status)
    • 200: succeeded
    • 404: failed
You can include the x-fc-status field in response headers to report to Function Compute whether the local function is successfully invoked.
  • If you do not specify a value for the x-fc-status field, Function Compute considers the invocation successful. If an error occurs during function execution, the system does not report the error to Function Compute. In this case, the business logic may not be affected, but the observability of Function Compute is affected. The following figure provides an example.image8hanshujisuanruntime
  • If you specify a value for the x-fc-status field, the system reports function invocation failures to Function Compute by using the x-fc-status field. If an error occurs during function invocation, the system records the error stack information in logs. The following figure provides an example.image9runtimefc
Note We recommend that you specify the StatusCode and x-fc-status fields in the HTTP response.

Sample code

In the following Node.js Express example, the POST method and /initialize path are called by Function Compute when a function instance is initialized. The POST method and /invoke path serve as handlers when Function Compute is called. The context and event parameters are obtained from req.headers and req.body, and the results of the invocation are then returned as an HTTP response.

'use strict';

const express = require('express');

// The constants.
const PORT = 9000;
const HOST = '0.0.0.0';
const app = express();
// Parse the JSON-formatted request body.
app.use(express.json({type:['application/json', 'application/octet-stream']}))


// The example of the Initializer hook. You must configure the initializer hook when you create the function.
app.post('/initialize', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, /initialize\n');
});


// Invoke the event function.
app.post('/invoke', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, event function\n');
});

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

server.timeout = 0; // Never time out.
server.keepAliveTimeout = 0; // keepalive, never timeout

Examples for other programming languages

You can use Serverless Devs to migrate your applications to Function Compute with a few clicks. The following example shows how to use Serverless Devs to deploy and invoke a function in an efficient manner. You can modify the sample code based on your business requirements.

FAQ