HTTP methods that Node.js supports, provided as an array of lowercase method names
npx @tessl/cli install tessl/npm-methods@1.1.00
# Methods
1
2
Methods 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.
3
4
## Package Information
5
6
- **Package Name**: methods
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install methods`
10
11
## Core Imports
12
13
```javascript
14
var methods = require('methods');
15
```
16
17
ES Modules (via transpilation/bundling):
18
19
```javascript
20
import methods from 'methods';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var methods = require('methods');
27
28
// methods is an array of lowercase HTTP method names
29
console.log(methods);
30
// ['get', 'post', 'put', 'head', 'delete', 'options', 'trace', ...]
31
32
// Check if a method is supported
33
var isSupported = methods.indexOf('get') !== -1;
34
35
// Use in HTTP routing
36
function isValidMethod(method) {
37
return methods.indexOf(method.toLowerCase()) !== -1;
38
}
39
40
// Use for validation in web frameworks
41
if (methods.indexOf(req.method.toLowerCase()) === -1) {
42
throw new Error('Unsupported HTTP method');
43
}
44
```
45
46
## Architecture
47
48
Methods has a simple, minimalist architecture designed for maximum compatibility:
49
50
- **Runtime Detection**: Uses feature detection to check for `http.METHODS` availability
51
- **Fallback Strategy**: Provides a curated list of 27 HTTP methods for older Node.js versions and browsers
52
- **Static Export**: Returns a pre-computed array that remains constant throughout application lifecycle
53
- **Zero Dependencies**: Self-contained with no external dependencies, reducing bundle size and security surface
54
55
The module exports a single array that is determined at load time based on the JavaScript environment.
56
57
## Capabilities
58
59
### HTTP Methods Array
60
61
The main and only export of the methods package - an array of lowercase HTTP method names.
62
63
```javascript { .api }
64
/**
65
* Array of lowercase HTTP method names supported by Node.js
66
* @type {string[]}
67
*/
68
var methods = require('methods');
69
```
70
71
**Runtime Behavior:**
72
- **Node.js 0.11+**: Returns `http.METHODS.map(method => method.toLowerCase())`
73
- **Node.js 0.10 and below**: Returns predefined array of 27 HTTP methods
74
- **Browser environments**: Returns predefined array (http module not available)
75
76
**Supported HTTP Methods:**
77
78
The array contains all standard and extended HTTP methods in lowercase:
79
80
- **Standard methods**: `get`, `post`, `put`, `head`, `delete`, `options`, `trace`, `patch`, `connect`
81
- **WebDAV methods**: `copy`, `lock`, `mkcol`, `move`, `propfind`, `proppatch`, `unlock`, `mkactivity`, `checkout`, `merge`
82
- **Other methods**: `purge`, `report`, `m-search`, `notify`, `subscribe`, `unsubscribe`, `search`
83
84
**Properties:**
85
- **Type**: Array of strings
86
- **Mutability**: Should be treated as read-only
87
- **Length**: Varies by Node.js version (27 methods in fallback, more in newer Node.js versions)
88
- **Content**: All method names are lowercase
89
- **Ordering**: Consistent within same Node.js version
90
91
**Usage Examples:**
92
93
```javascript
94
var methods = require('methods');
95
96
// Iterate through all methods
97
methods.forEach(function(method) {
98
console.log('Supported method:', method);
99
});
100
101
// Check if specific method exists
102
function hasMethod(methodName) {
103
return methods.indexOf(methodName.toLowerCase()) !== -1;
104
}
105
106
// Filter custom methods
107
var standardMethods = methods.filter(function(method) {
108
return ['get', 'post', 'put', 'delete'].indexOf(method) !== -1;
109
});
110
111
// Use in express-like routing
112
function createHandler(method, path, handler) {
113
if (!hasMethod(method)) {
114
throw new Error('Invalid HTTP method: ' + method);
115
}
116
// ... routing logic
117
}
118
```
119
120
## Types
121
122
```javascript { .api }
123
/**
124
* The methods export type definition
125
* @typedef {string[]} Methods
126
* @description Array of lowercase HTTP method name strings
127
*/
128
```
129
130
## Error Handling
131
132
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:
133
134
- Node.js itself fails to load the `http` module (extremely rare)
135
- The module file is corrupted or inaccessible
136
137
No runtime validation or error checking is performed on the exported array.
138
139
## Browser Compatibility
140
141
The package works in browser environments via browserify or similar bundlers:
142
143
```javascript
144
// Works in browsers - http module is shimmed out
145
var methods = require('methods');
146
// Returns the predefined fallback list
147
```
148
149
## Version Information
150
151
- **Current Version**: 1.1.2
152
- **Node.js Support**: >= 0.6
153
- **License**: MIT
154
- **Repository**: jshttp/methods
155
156
## Related Packages
157
158
This package is commonly used as a dependency in:
159
- HTTP frameworks (Express, Koa, etc.)
160
- Routing libraries
161
- HTTP clients and servers
162
- Method validation utilities
163
- Web development tools