All Products
Search
Document Center

Function Compute:HTTP handler

Last Updated:Nov 27, 2023

You can use HTTP handlers to process HTTP requests in an efficient manner. When a function is invoked, Function Compute uses the handler that you specify to process the HTTP request. This topic describes the structure of HTTP handlers in a Node.js runtime environment and provides examples on how to use HTTP handlers in a Node.js runtime environment.

HTTP handler signature

The following sample code provides an example on the signature of an HTTP handler in a Node.js runtime environment. You need to only 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>');
}     

Description

  • handler: the name of the HTTP handler.

  • req: the HTTP request struct.

  • resp: the HTTP response struct.

  • context: the context information. For more information, see Context.

HTTP request struct

Field

Type

Description

headers

Object

Stores the key-value pairs that are sent by HTTP clients.

path

String

Specifies 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

Specifies the HTTP method.

clientIP

String

Specifies the IP address of the client.

url

String

Specifies the URL of the request.

Note

The key in the header that contains one of the following fields or starts with x-fc- is ignored. Therefore, you cannot customize the key.

  • connection

  • keep-alive

HTTP response struct

Method

Type

Description

response.setStatusCode(statusCode)

interger

Sets the status code.

response.setHeader(headerKey, headerValue)

String, String

Sets a response header.

response.deleteHeader(headerKey)

String

Deletes a response header.

response.send(body)

Buffer, String, and Stream.Readable

Sends the response body.

Important

The headerKey that contains one of the following fields or starts with x-fc- is ignored. Therefore, you cannot customize the header key.

  • connection

  • content-length

  • date

  • keep-alive

  • server

  • upgrade

Limits

  • Request limits

    If a request exceeds one of the following limits, the system returns status code 400 and error InvalidArgument.

    Field

    Description

    HTTP status code

    Error code

    headers

    The total size of the keys and values in the request headers cannot exceed 8 KB.

    400

    InvalidArgument

    path

    The total size of the request path and query parameters cannot exceed 4 KB.

    body

    The total size of the body of a synchronous invocation request cannot exceed 32 MB. The total size of the body of an asynchronous invocation request cannot exceed 128 KB.

  • Response limits

    If a response exceeds one of the following limits, the system returns status code 502 and error BadResponse.

    Field

    Description

    HTTP status code

    Error code

    headers

    The total size of the keys and values in the response headers cannot exceed 8 KB.

    502

    BadResponse

Example: Obtain the details of the HTTP request and return the body

Sample code

module.exports.handler = function (request, response, context) {
    // get request 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.setStatusCode(200)
    response.setHeader('content-type', 'application/json')
    response.send(respBody)
};

Before you start

Create a service

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, click the desired service.

  3. On the Functions page, click Create Function.

    Create an HTTP function whose runtime environment is Node.js 14.x. For more information, see Create a function.

  4. On the function details page, click the Code tab, enter the preceding sample code in the code editor, and then click Deploy.

    Note

    In the preceding sample code, the handler of the function is the handler method in index.js. If you specify a different handler, use the actual file and method. For more information, see Lifecycle hooks for function instances.

  5. On the Code tab, click the down icon next to Test Function and select Configure Test Parameters from the drop-down list.

  6. In the Configure Test Parameters panel, click Create New Test Event or Modify Existing Test Event, set the following parameters, and then click OK.

    • Request Name: Enter a custom request name.

    • Request Method: Select POST.

    • Request Path: Enter ?foo=bar.

    • Request Body: Enter hello,fc in the code editor.

  7. Click Test Function.

    After the function is executed, the information such as the client IP address is contained in the returned result.