ciochPep
ciochPep

Reputation: 2592

expressjs: how can I use bodyParser only for a specific resource?

I am using expressjs, I would like to use the body Parser only for specific resource, how can I do that?

Upvotes: 0

Views: 233

Answers (2)

Renato Gama
Renato Gama

Reputation: 16519

@Matt answer is good, you can optimize its syntax by writing:

app.get('/foo', express.bodyParser, function (req, res, next) {
    // parsed request
});

Upvotes: 1

Matt
Matt

Reputation: 75307

app.use() allows you to specify a "mount path", as well as which middleware to mount, so you should be able to get away with;

app.use('/foo', express.bodyParser);

As express.bodyParser returns a function whose signature is req, res, next (as with all middleware), this seems analagous to adding it as a handler to a resource;

app.get('/foo', express.bodyParser);
app.get('/foo', function (req, res, next) {
    // req has been parsed.
});

Upvotes: 1

Related Questions