When you use Node.js for programming in Function Compute, you must define a Node.js function as the handler function. This topic describes the structure and features of Node.js HTTP functions. After you configure an HTTP trigger for a function, the HTTP trigger sends an HTTP request to trigger the execution of the function. This allows you to quickly build web applications.

Definition

The following example shows the definition of a simple Node.js HTTP function:

function(request, response, context)      
The function definition contains the following parts:

Request structure

Field Type Description
headers Object Stores a key-value pair from an HTTP client that sends the HTTP request.
path String Specifies the path of the HTTP request.
queries Object Stores the key-value pair of the queries parameters of the HTTP request path. The parameter value can be a string or an array.
method String The HTTP method that is used in the request.
clientIP String The IP address of the HTTP client.
url String The URL of the request.

In addition, the request parameter in HTTP functions is compatible with HTTP requests. The body field is not provided. To query the body field, you must send an HTTP request. For more information, see Node.js.

// The following example shows how to query the body field.
var getRawBody = require('raw-body')
getRawBody(request, function(err, data){
      var body = data
})            
Note

The following Headers Key fields are ignored, and cannot be customized. The Key fields that start with x-fc- are also ignored.

  • accept-encoding
  • connection
  • keep-alive
  • proxy-authorization
  • te
  • trailer
  • transfer-encoding

Methods in the response

  • response.setStatusCode(statusCode): specifies the status code.
    • param statusCode: (required, type integer)
  • response.setHeader(headerKey, headerValue): specifies the header in the response.
    • paramheaderKey: (required, type string)
    • paramheaderValue: (required, type string)
  • response.deleteHeader(headerKey): deletes the header from the response.
    • paramheaderKey: (required, type string)
  • response.send(body): sends the body of the response.
    • parambody: (required, typeBuffer or a string or a stream.Readable).
Note

The following Headers Key fields are ignored, and cannot be customized. The Key fields that start with x-fc- are also ignored.

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

Limits

  • Request limits

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

    Field Limit HTTP status code Error code
    headers The total size of the keys and values in the request header cannot exceed 4 KB. 400 InvalidArgument
    path The total size of the request path and queries parameter cannot exceed 4 KB.
    body The body of the HTTP response cannot exceed 6 MB.
  • Response limits

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

    Field Limit HTTP status code Error code
    headers The total size of the keys and values in the response header cannot exceed 4 KB. 502 BadResponse
    body The body of the HTTP response cannot exceed 6 MB.

Example

The following sample code shows how to query the details about a request from a function.

var getRawBody = require('raw-body')
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

    // get request body
    getRawBody(request, function (err, data) {
        var body = data
        // you can deal with your own logic here

        // set response
        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)
    })
};
            

References