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. |
Notice
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.setStatusCode(200)
response.setHeader('content-type', 'application/json')
response.send(respBody)
};