Reputation: 5729
I have a middleware defined like
app.use('api/', authenticate, bar);
Inside the authenticate function, I am attaching a variable in the req.body
like
req.body.user = foo;
But when I do a console.log(req.body.user)
inside bar; I found undefined.
However if I attach the variable like req.user = foo
and then inside the bar
function I do a console.log(req.user)
it successfully printing foo
. Is there any reason the variable attached req.body is losing what is attached to it on its way?
Upvotes: 2
Views: 1997
Reputation: 5392
Works just fine ...
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use('*', function(req, res, next){
console.log("Middlewarez");
req.body.user = { name: "John", last: "Smith" }
next();
}, function(req, res){
console.log("Handler")
console.log(req.body); // => { user: { name: 'John', last: 'Smith' } }
res.end("Done");
})
app.listen(8080);
Perhaps you're not using the body-parser?
Upvotes: 2