Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns
Node.js http.METHODS and http.STATUS_CODES
Reference for http.METHODS and http.STATUS_CODES in Node.js: the built-in tables of HTTP methods the parser knows and the reason phrase for each status code.
The node:http module exports two small lookup tables. http.METHODS is a sorted array of the HTTP method names that Node's parser understands. http.STATUS_CODES is an object that maps each numeric status code to its standard reason phrase, for example 404 to 'Not Found'.
They are plain data: handy for validating an incoming method, for printing readable log lines, and for building error responses without hard-coding text.
import http from 'node:http'; console.log(http.STATUS_CODES[404]); // 'Not Found' console.log(http.METHODS.includes('PATCH')); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
http.METHODS: string[] http.STATUS_CODES: { [code: number]: string }
| Name | Description |
|---|---|
http.METHODS |
Array of upper-case method names, in alphabetical order. It includes the common ones (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS), CONNECT and TRACE, and many WebDAV, source-control and other methods such as PROPFIND, MKCOL, PURGE and QUERY. |
http.STATUS_CODES |
Object with code keys and phrase values. Includes 1xx through 5xx codes, such as 103 ('Early Hints'), 418 and 511. |
Returns: these are properties, not functions. Looking up a code that is not in the table gives undefined.
Errors: none; the tables are read-only data and never throw.
Usage notes
- The list is the parser's list. A method that is in
http.METHODScan be received byhttp.createServer()without a400 Bad Requestfrom the parser. A method outside the list is rejected before your handler runs. - Being in the list does not mean your server supports it. Decide for yourself which methods a route accepts and answer
405 Method Not Allowedwith anAllowheader for the rest. - Comparisons are case-sensitive.
req.methodis upper-case as received; the parser does not accept lower-caseget. - Reason phrases are optional in HTTP/2 and ignored by clients in HTTP/1.1. They are for humans;
res.statusMessagedefaults to the table value when you only setstatusCode. - Do not use the phrase as an error message to users without thought:
'Internal Server Error'hides the real problem from you in logs, so log the actual error separately. - Both tables can change between Node.js versions as new methods and codes are standardized (the list gained
QUERYwith newer parsers). Do not hard-code the length. - HTTP/2 has its own constants in
http2.constants(HTTP2_HEADER_*,HTTP_STATUS_*).