Reputation: 1988
I have an Express server that manages a login form page:
const app = express();
// section A
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded());
app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);
It runs correctly.
Then I have another controller that reads RAW DATA:
// section B
const concat = require('concat-stream');
app.use(function(req, res, next) {
req.pipe(concat(function(data: any) {
req.body = data;
next();
}));
});
app.post('*', otherController.post);
export let post = (req: any, res: Response) => {
console.log(req.body); //i see RAW DATA
res.end('n');
}
It also works well.
But if I join the two sections, the second section stops working.
How can I say to use the req.pipe
only for the section B?
Upvotes: 1
Views: 1539
Reputation: 1988
This middleware (like virtually every other middleware out there), does not have a way to exclude it from certain requests, since the method in which users would desire to exclude can be anything--url, header, query string, loaded user, and more. Instead middleware places the logic of pathing on you: either (a) explicitly include it (which means placing on the routes, as recommended in our documentation) or (b) wrap the middleware with your own exclusion logic:
const parseExtend = bodyParser.urlencoded({ extended: true });
app.use((req, res, next) => shouldParseRequest(req) ? parseExtend(req, res, next) : next());
/* implement shouldParseRequest (req) to return false for whatever you want to not parse json for */
Upvotes: 1