Node.js HTTP Request Parser
raw JSON →node-req is a minimalist I/O module designed to parse and extract specific values from Node.js's native `http.IncomingMessage` object, explicitly designed with no side-effects. It provides a set of helper methods for accessing common request components such as query strings, HTTP method, headers, IP addresses, protocol (HTTP/HTTPS), subdomains, and content negotiation details. Currently at version 2.1.2, this package has not received updates in approximately six years, indicating it is no longer actively maintained. Its core differentiation lies in its pure parsing approach, making no assumptions about routing or higher-level HTTP handling, thus allowing it to integrate into various Node.js server architectures as a low-level utility. It leverages the `qs` package internally for robust query string parsing.
Common errors
error TypeError: nodeReq.get is not a function ↓
const nodeReq = require('node-req') for importing in a CommonJS context. If in an ESM project, consider using import * as nodeReq from 'node-req' or migrating to an ESM-compatible alternative. error ERR_REQUIRE_ESM ↓
import/export and removing "type": "module" from its nearest package.json), or use a dynamic import (const nodeReq = await import('node-req')) within the ESM file, then access its properties (nodeReq.default.get(req)). The best long-term fix is to migrate to an actively maintained ESM-compatible library. Warnings
breaking The `request` package, a popular HTTP client which shares a similar name, was fully deprecated in February 2020. While `node-req` is a distinct I/O parsing library and not a client, its lack of maintenance means it may contain unfixed bugs or security vulnerabilities and could become incompatible with newer Node.js versions. ↓
gotcha This package is exclusively CommonJS (`require`) and does not support ES Modules (`import`). Attempting to use `import` syntax will result in `ERR_REQUIRE_ESM` or similar module resolution errors in projects configured for ESM. ↓
gotcha The package has not been updated in approximately six years. This means it may not be compatible with the latest Node.js runtime features or security best practices, and any new issues discovered will likely not be addressed. ↓
Install
npm install node-req yarn add node-req pnpm add node-req Imports
- nodeReq wrong
import nodeReq from 'node-req'correctconst nodeReq = require('node-req') - get wrong
import { get } from 'node-req'correctconst { get } = require('node-req') - method wrong
import { method } from 'node-req'correctconst { method } = require('node-req')
Quickstart
const http = require('http');
const nodeReq = require('node-req');
const server = http.createServer((req, res) => {
// Parse query string (e.g., /?name=Alice&age=30)
const query = nodeReq.get(req);
const name = query.name || 'Guest';
// Get HTTP method (e.g., GET, POST)
const method = nodeReq.method(req);
// Get all request headers
const headers = nodeReq.headers(req);
const userAgent = nodeReq.header(req, 'user-agent');
// Get client IP address
const ip = nodeReq.ip(req, true); // `true` for trusting proxy headers
// Check if the request has a body (e.g., for POST, PUT)
const hasBody = nodeReq.hasBody(req);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(
`Hello, ${name}!\n` +
`Method: ${method}\n` +
`User-Agent: ${userAgent}\n` +
`Client IP: ${ip}\n` +
`Has Body: ${hasBody}\n` +
`Full Query: ${JSON.stringify(query)}\n`
);
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
console.log('Try visiting: http://localhost:3000/?name=Bob&city=NewYork');
});