Instructions for the http.response.writeHead method in node.js

  • 2020-05-05 10:52:01
  • OfStack

method description:

Sends the response header to the client of the request.

This function can only be called once in a request, and if it is not called, a response header is automatically generated.

syntax:


response.writeHead(statusCode, [reasonPhrase], [headers])

receive parameters:

statusCode                     HTTP status codes, such as 200(request successful), 404 (not found), etc.

reasonPhrase

headers                  

Example of :


var body = 'hello world';
response.writeHead(200, {
  'Content-Length': body.length,
  'Content-Type': 'text/plain'
});


Related articles: