You can use HTTP request handlers to handle HTTP requests more easily. When calling a function, you Function Compute use the execution method you provide to handle the HTTP request. This topic describes the structural features and examples of Node.js HTTP request handlers.

HTTP Handler Signature

The signature of the HTTP handler for Node.js is as follows: You only need to implement one function to respond to HTTP requests.

exports.handler = (req, resp, context) => {
    console.log("receive body: ", req.body.toString());
    resp.setHeader("Content-Type", "text/plain");
    resp.send('<h1>Hello, world!</h1>');
The example is parsed as follows:
  • handler: the name of the HTTP handler.
  • req: the structure of an HTTP request.
  • resp :HTTP return structure.
  • context: context information. For more information, see Context.

HTTP request structure

Sub-attribute Job type Description
headers Object Stores the key-value pairs that are sent by the HTTP client.
path String The HTTP path.
queries Object Stores the key-value pairs of query parameters in the HTTP path. The parameter value can be a string or an array.
method String The HTTP method.
clientIP String The IP address of the client.
url String The URL of the request.
Note If the key in the header key-value pair contains the following fields or key starting with x-fc- are ignored. Therefore, you cannot customize the fields.
  • accept-encoding
  • connection
  • keep-alive
  • proxy-authorization
  • te
  • trailer
  • transfer-encoding

HTTP response structure

Method Job type Description
response.setStatusCode(statusCode) interger Set the status code.
response.setHeader(headerKey, headerValue) String,String Set the response header.
response.deleteHeader(headerKey) String Deletes a response header.
esponse.send(body) Buffer,String,Stream.Readable Send the response body.

headerKey headerKey that contain the following fields or start with x-fc- are ignored. Therefore, you cannot customize the fields.

  • connection
  • content-length
  • content-encoding
  • date
  • keep-alive
  • proxy-authenticate
  • server
  • trailer
  • transfer-encoding
  • upgrade

Example: Obtain HTTP Request Details and Return Body

Sample code

module.exports.handler = function (request, response, context) {
    // get requset header
    var reqHeader = request.headers
    var headerStr = ' '
    for (var key in reqHeader) {
        headerStr += key + ':' + reqHeader[key] + '  '

    // get request info
    var url = request.url
    var path = request.path
    var queries = request.queries
    var queryStr = ''
    for (var param in queries) {
        queryStr += param + "=" + queries[param] + '  '
    var method = request.method
    var clientIP = request.clientIP
    var body = request.body
    var respBody = new Buffer('requestHeader:' + headerStr + '\n' + 'url: ' + url + '\n' + 'path: ' + path + '\n' + 'queries: ' + queryStr + '\n' + 'method: ' + method + '\n' + 'clientIP: ' + clientIP + '\n' + 'body: ' + body + '\n')
    response.setHeader('content-type', 'application/json')


Create a Service


  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
  4. On the Services page, click the target service.

  5. Create an HTTP function whose runtime environment is Node.js 14.x. For more information, see Create a function.
  6. On the Function Details page, click the Function Code tab, enter the preceding sample code in the code editor, and then click Save and Deploy.
    Note The request handler for the function in the preceding sample code is the handler method in the index.js. If the request handler (function entry) configuration of your function is different, find the corresponding file and method to update it.
  7. On the Function Code tab, click the down icon next to Test Function and select Configure Test Parameters from the drop-down list.
  8. In the Configure Test Parameters panel, select Create Test Request or Edit Existing Test Request, set the following parameters, and then click OK.
    • Request Name: Enter a custom request name.
    • Request Method: Select POST.
    • Request Path: Enter a ?foo=bar.
    • Request Body: Enter the hello,fc in the code box.
  9. Click Test Function.
    After the function is successfully executed, you can see the client IP address and other information contained in the response.