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 function can directly process HTTP requests, facilitating creation of web applications.

Definition

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

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

Request structure

Parameter Type Description
headers Map Stores a key-value pair from an HTTP client that sends the HTTP request.
path String Specifies the path of the HTTP request.
queries Map 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 are compatible with HTTP requests. The body field is not provided. To obtain the body field, you must send an HTTP request.

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

The following Headers Key fields are ignored because Function Compute provides these fields by default, which cannot be defined by users. The Key fields that start with x-fc- are also ignored.

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

Methods provided 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, type Buffer or a string or a stream.Readable)
Note

The following Headers Key fields are ignored because Function Compute provides these fields by default, which cannot be defined by users. 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 status code 400 and 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 query parameters cannot exceed 4 KB.
    body The body of the HTTP request cannot exceed 6 MB.
  • Response limits

    If a response exceeds any one of the following limits, the system returns status code 502 and 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 obtain the details about a request from a function.

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

    // get request body
    getRawBody(request, function (err, data) {
        var body = data
        // Compile your business logic

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

More information