als-body-parser
v1.0.0
Published
A flexible, efficient body parsing middleware for Node.js, supporting JSON, URL-encoded, and plain text, with customizable options for request handling.
Downloads
5
Maintainers
Readme
als-body-parser
als-body-parser
is a flexible middleware for parsing incoming request bodies in a Node.js environment, making it easy to integrate with frameworks like Express or with the native HTTP server. It adds a req.body
property to the incoming request object, which can be a JSON object, URL-encoded string, or plain text, depending on the Content-Type of the request.
Installation
Install using npm:
npm install als-body-parser
Basic Usage
With Native HTTP Server
const http = require('http');
const bodyParser = require('als-body-parser')({});
// Create HTTP server and use bodyParser middleware
http.createServer((req, res) => {
bodyParser(req, res, () => {
if (req.body) {
// Send parsed body back as JSON
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(req.body));
} else {
// Handle cases where no body could be parsed
res.writeHead(400);
res.end('No body parsed');
}
});
}).listen(3000);
With Express
const express = require('express');
const bodyParser = require('als-body-parser')({});
const app = express();
app.use(bodyParser);
app.post('/data', (req, res) => {
res.json(req.body);
});
app.listen(3000);
Advanced Usage
Configuration Options
als-body-parser
can be customized with several options:
supportedMethods
: Array of HTTP methods to parse (default:['POST', 'PUT', 'PATCH']
).supportedCt
: Array of supported content types (default:['application/x-www-form-urlencoded', 'application/json', 'text/plain']
).limit
: Maximum allowed size of the request body in bytes (default:1048576
).timeout
: Timeout in milliseconds for receiving the request body (default:5000
).logger
: Function to log errors (default:console.log
).httpErrorHandler
: Function to handle HTTP errors, expects(res, status, message)
(default handles by setting response headers and ending the request).
Customizing Behavior
Changing Supported Methods and Content Types
const bodyParser = require('als-body-parser')({
supportedMethods: ['POST'],
supportedCt: ['application/json']
});
app.use(bodyParser);
Custom Error Handling and Logging
const bodyParser = require('als-body-parser')({
httpErrorHandler: (res, status, message) => {
res.status(status).send({ error: message });
},
logger: (error) => customLogger.error(error)
});
app.use(bodyParser);
Status Codes
The middleware can terminate the request with the following status codes under certain conditions:
413
(Content Too Large): If the body exceeds the specifiedlimit
.400
(Bad Request): If the body cannot be parsed correctly (e.g., malformed JSON).408
(Request Timeout): If the full body is not received within the specifiedtimeout
.
req.body
Depending on the Content-Type
, req.body
may be:
- An object, for JSON requests.
- A string, for URL-encoded or plain text requests.
- Undefined, if the content type is not supported or no body data is provided.