After sending response I'm calling next()
which goes to my post middleware. Where I want to get data which I'm sending data in response.
res.status(200).json({username: 'abc'});
next()
now In post middleware I want to access that data.
I have tried res.on('data' ,function(chunk){})
but no luck.
also if do res.someProperty = "some data"
it works fine.
But i cant do in case of json as my code already written I have to fetch that data using middleware as i have 100s of routes and i dont want to change each. So the only way to access data is by using middleware.
So my purpose is to get data added in json. something like res.username or res.data.username.
App.js middleware code
//pre-middleware
app.use(function(req, res, next){
res.on('data', function(err, data){
if(err) console.log(err);
console.log('data', data);
})
console.log('######pre status', res.statusCode);
next();
});
app.use('/', index);
//post-middleware
app.use(function(req, res, next){
console.log('###########post');
console.log( 'response', res.statusMessage);
})
index.js get router code
router.get('/middleware', function(req, res, next){
res.status(201).json({
username: 'awais'
});
next();
});
Someone add solution related to printing body and I mention that first I don't want to print body of response secondly those solution tried and not working.