HTTP methods that Node.js supports, provided as an array of lowercase method names
npx @tessl/cli install tessl/npm-methods@1.1.0Methods is a utility library that provides an array of lowercase HTTP method names supported by Node.js. It serves as a cross-platform compatibility layer by using Node.js core's http.METHODS when available, or falling back to a predefined list for older Node.js versions and browser environments.
npm install methodsvar methods = require('methods');ES Modules (via transpilation/bundling):
import methods from 'methods';var methods = require('methods');
// methods is an array of lowercase HTTP method names
console.log(methods);
// ['get', 'post', 'put', 'head', 'delete', 'options', 'trace', ...]
// Check if a method is supported
var isSupported = methods.indexOf('get') !== -1;
// Use in HTTP routing
function isValidMethod(method) {
return methods.indexOf(method.toLowerCase()) !== -1;
}
// Use for validation in web frameworks
if (methods.indexOf(req.method.toLowerCase()) === -1) {
throw new Error('Unsupported HTTP method');
}Methods has a simple, minimalist architecture designed for maximum compatibility:
http.METHODS availabilityThe module exports a single array that is determined at load time based on the JavaScript environment.
The main and only export of the methods package - an array of lowercase HTTP method names.
/**
* Array of lowercase HTTP method names supported by Node.js
* @type {string[]}
*/
var methods = require('methods');Runtime Behavior:
http.METHODS.map(method => method.toLowerCase())Supported HTTP Methods:
The array contains all standard and extended HTTP methods in lowercase:
get, post, put, head, delete, options, trace, patch, connectcopy, lock, mkcol, move, propfind, proppatch, unlock, mkactivity, checkout, mergepurge, report, m-search, notify, subscribe, unsubscribe, searchProperties:
Usage Examples:
var methods = require('methods');
// Iterate through all methods
methods.forEach(function(method) {
console.log('Supported method:', method);
});
// Check if specific method exists
function hasMethod(methodName) {
return methods.indexOf(methodName.toLowerCase()) !== -1;
}
// Filter custom methods
var standardMethods = methods.filter(function(method) {
return ['get', 'post', 'put', 'delete'].indexOf(method) !== -1;
});
// Use in express-like routing
function createHandler(method, path, handler) {
if (!hasMethod(method)) {
throw new Error('Invalid HTTP method: ' + method);
}
// ... routing logic
}/**
* The methods export type definition
* @typedef {string[]} Methods
* @description Array of lowercase HTTP method name strings
*/The methods package does not throw any exceptions during normal operation. It provides a static array that is determined at module load time and remains constant throughout the application lifecycle. The package will only fail if:
http module (extremely rare)No runtime validation or error checking is performed on the exported array.
The package works in browser environments via browserify or similar bundlers:
// Works in browsers - http module is shimmed out
var methods = require('methods');
// Returns the predefined fallback listThis package is commonly used as a dependency in: