HTTP
request.method
On an incoming server request, provides the parsed HTTP method. A route should check the method as well as its path; a recognized method is not proof the caller is authorized. Tested on Node.js 22.16.0. Example output (one line per entry): ["405 GET POST"].
Formal Syntax & Structure
nodejs syntaxnodejs
request.methodReturn Value
string
HTTP method for an incoming request.
Working Example
Example Snippetnodejs
import http from 'node:http';const server = http.createServer((req, res) => { if (req.method !== 'GET') { res.statusCode = 405; res.setHeader('Allow', 'GET');} res.end(req.method); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try { const r = await fetch(url, { method: 'POST' }); console.log(r.status, r.headers.get('allow'), await r.text());}finally { server.closeAllConnections(); await new Promise(r => server.close(r));}