HTTP Methods Constants

1.1.0 · active · verified Wed Apr 22

http-methods-constants is a lightweight utility package that provides standard HTTP method verbs as string constants. Its primary purpose is to eliminate "magic strings" in codebases, improving readability and reducing potential typos when referring to methods like 'GET', 'POST', 'PUT', or 'DELETE'. The current stable version is 1.1.0. Given its fundamental nature, it is expected to have a very stable and infrequent release cadence, with new versions primarily occurring for minor improvements or ecosystem compatibility updates rather than new features. It differentiates itself through its extreme simplicity and singular focus, offering a straightforward object of string constants without additional logic or validation, making it an ideal drop-in for projects needing explicit HTTP method references.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the constants object and iterating through all available HTTP methods, followed by a conceptual example of using them in a request handler for clarity.

const httpMethods = require('http-methods-constants');

console.log("All supported HTTP methods:");
for (const methodKey in httpMethods) {
  if (Object.prototype.hasOwnProperty.call(httpMethods, methodKey)) {
    console.log(`- ${methodKey}: ${httpMethods[methodKey]}`);
  }
}

// Example usage in an Express.js-like route handler (conceptual)
function handleRequest(method, path, body) {
  if (method === httpMethods.GET && path === '/api/data') {
    console.log(`Handling GET request for ${path}`);
    return { status: 200, data: 'Some data' };
  } else if (method === httpMethods.POST && path === '/api/items') {
    console.log(`Handling POST request for ${path} with body:`, body);
    return { status: 201, message: 'Item created' };
  }
  return { status: 404, message: 'Not Found' };
}

console.log(handleRequest('GET', '/api/data'));
console.log(handleRequest('POST', '/api/items', { name: 'New Item' }));

view raw JSON →