req-body-parser
v1.1.2
Published
A lightweight JSON body parser middleware for Express
Maintainers
Readme
req-body-parser
A lightweight JSON body parser middleware for Express.
Works similarly to express.json() but gives you full control with minimal dependencies.
Installation & Usage
// Install the package first:
npm install req-body-parser
const express = require('express');
const reqBodyParser = require('req-body-parser'); // require from npm
const app = express();
/**
* Use the middleware
*/
app.use(reqBodyParser());
// Example POST route
app.post('/signup', (req, res) => {
// req.body now contains parsed JSON
const username = req.body.username;
res.json({ username });
console.log(username);
});
// Start server
app.listen(3000, () => {
console.log('Server running on port 3000');
});
/**
* Options:
* limit: number (default 1e6)
*
* Behavior:
* - Payload larger than limit returns 413
* - Invalid JSON returns 400
*
* Test with curl:
* curl -X POST http://localhost:3000/signup \
* -H "Content-Type: application/json" \
* -d '{"username":"biswash"}'
*/