Reputation: 319
My Express 'end' event is not emitted. Please shred some hints on how to fix it.
Express setup:
//Enable CORS
console.log("Enabled CORS and all pre-flight request");
app.use(cors())
app.options('*', cors()) // enable pre-flight request for ALL request
app.use(bodyParser.json({ type: 'application/*+json' })); //James 11/7/2017
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
Router setup:
//update
app.put('/:resourceType/id/:idPatient', function (req, res, next) {
console.log("update by Id [" + req.params.resourceType + "][" + req.params.idPatient + "][" + JSON.stringify(req.body) + "]");
gModel.fhirUpdateById(req, res, next, false); // isDeleted = false
});
The console.log
above can be seen every time,
// Handler
module.exports.fhirUpdateById = function (req, res, next, isDeleted) {
var body = '';
//Test 1: Direct call without req.on, then can't parse the body
//updateById((req.params.resourceType, req.params.resourceId, res, next, req.body, isDeleted));
//Test 2:
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log("fhirUpdateById Resource type: [" + req.params.resourceType + "] Body: [" + body + "]");
updateById((req.params.resourceType, req.params.resourceId, res, next, body, isDeleted));
});
}
The console.log
above is never shown. The req.on('end', function () ...)
have never been fired up ;)
Questions:
1. How to fix the req.on('end'...) triggering?
2. Should I put the req.on('data') and req.on('end') on the app.use() since I would like to get the whole request on various PUT and POST?
3. If I don't use the req.on, will I get a partial body even though my body is less than 1MB?
Please let me know if you need more information. Thanks.
========================================================
Resolved:
Please see Sandip's solution below.
In summary,
for http PUT, we should use either bodyParser.json or req.on('end', function), but not both. For http POST, we should use both;
the returned result from bodyParser.json is req.body which is a JSON object while req.on('end', function) is returning a string in my code;
Thanks ALL!
Upvotes: 1
Views: 6775
Reputation: 713
FYI - found out that requiring express.urlencoded on a route can also cause this same problem. I ended up reorganizing my routes to separate out the affected route (into a separate file and route) from the routes that needed urlencoded.
Upvotes: 1
Reputation: 719
Found the issue. If you use body parser middleware, then none of the data
or end
event on request object is fired. I removed the body parser from your code, and the events started firing. Actually body parser does the job for you that you are trying to achieve through data
event listener.
So, you either remove the body parser entirely
//Enable CORS
console.log("Enabled CORS and all pre-flight request");
app.use(cors())
app.options('*', cors()) // enable pre-flight request for ALL request
//update
app.put('/:resourceType/id/:idPatient', function (req, res, next) {
console.log("update by Id [" + req.params.resourceType + "][" + req.params.idPatient + "][" + JSON.stringify(req.body) + "]");
gModel.fhirUpdateById(req, res, next, false); // isDeleted = false
});
Or, you should use body parser and get the data from req.body
directly
To understand how it works, you can see this article
Upvotes: 4