Using body parser either
- application/x-www-form-urlencoded body parser
or
- json body parser
Yields the same results.
This is how I'm calling the API
$.ajax({ type:'post', url:'/api/order', headers: { 'GreatestHits': 'SteveMillerBand' }, data: { 'the': 'greatest' } });
Why isn't .on('data')
triggering? Isn't body
simply the data
object of the request, pretty much? I am seeing the form data logging to the console. I can also direct the post to to prove the point.
I can only believe the Express is the real c@ blocker. If that is not it then I at a loss.
// this is the code being called by the $.ajax()
app.use(function (req, res) {
console.log("app.use req.data", req.data);
req.on('data', function (chunk) {
console.log("req.use on data");
}).on('end', function () {
console.log("app.use on end");
});
res.sendStatus(200);
});
On the $.ajax()
post each of the following models, based on the above code, results as shown:
app.use(callback() {} )
- has req.body
- does not have req.data (undefined)
- will NOT trigger .on('data');
- will NOT trigger .on('end');
.
app.post(callback() {} )
- will not even be called unless a route string is included.
.
app.post('route', callback() {} )
- has
req.body
- does not have
req.data
(undefined)
- will NOT trigger
.on('data');
- will NOT trigger
.on('end');
.
app.use('route', callback() {} )
- has
req.body
- does not have
req.data
(undefined)
- will NOT trigger
.on('data');
- will trigger
.on('end');
Using body parser either
- application/x-www-form-urlencoded body parser
or
- json body parser
Yields the same results.
This is how I'm calling the API
$.ajax({ type:'post', url:'/api/order', headers: { 'GreatestHits': 'SteveMillerBand' }, data: { 'the': 'greatest' } });
Why isn't .on('data')
triggering? Isn't body
simply the data
object of the request, pretty much? I am seeing the form data logging to the console. I can also direct the post to http://httpbin/post
to prove the point.
I can only believe the Express is the real c@ blocker. If that is not it then I at a loss.
// this is the code being called by the $.ajax()
app.use(function (req, res) {
console.log("app.use req.data", req.data);
req.on('data', function (chunk) {
console.log("req.use on data");
}).on('end', function () {
console.log("app.use on end");
});
res.sendStatus(200);
});
On the $.ajax()
post each of the following models, based on the above code, results as shown:
app.use(callback() {} )
- has req.body
- does not have req.data (undefined)
- will NOT trigger .on('data');
- will NOT trigger .on('end');
.
app.post(callback() {} )
- will not even be called unless a route string is included.
.
app.post('route', callback() {} )
- has
req.body
- does not have
req.data
(undefined)
- will NOT trigger
.on('data');
- will NOT trigger
.on('end');
.
app.use('route', callback() {} )
Share Improve this question edited Mar 19, 2017 at 0:46 Steve asked Mar 18, 2017 at 19:06 SteveSteve 9232 gold badges8 silver badges32 bronze badges 15
- has
req.body
- does not have
req.data
(undefined)
- will NOT trigger
.on('data');
- will trigger
.on('end');
-
app.use
seems odd here, that's really more for middleware vs. route handling. Generally for routes you can use.use
to pull in something that knows how to handle specific routes. See expressjs./en/guide/routing.html – Joe Commented Mar 18, 2017 at 19:13 - Than you. But I believe the app.use() is required so we have re object to put the emitter req.on() pointing to the callback. This is taken from and based on a very typical code snips all over the internet. – Steve Commented Mar 18, 2017 at 19:15
-
Does Request even have an
on
method? It's doesn't seem to be documented and doesn't document any events. In fact, the only event I see listed on that entire page is Application'smount
. – T.J. Crowder Commented Mar 18, 2017 at 19:15 - @T.J.Crowder that might be a good catch. I didn't thank abut that. And when I inspect req. I don't see it. But I do see this code in a lot of places on the internet claiming to do what I'm attempting. So where could I be off? Versions of node maybe? – Steve Commented Mar 18, 2017 at 19:19
-
1
If
body-parser
is still active, it will read and exhaust the request stream, sodata
will never trigger for you (because there won't be any more data to read). Also: show how you configuredbody-parser
. – robertklep Commented Mar 18, 2017 at 21:27
1 Answer
Reset to default 5The ment thread is too long already to continue adding to it. Not knowing what better I can do I'm posting this as an answer to continue the conversation.
This code triggers .on('end')
app.use(function (req, res, next) {
console.log("app.use req.data", req.body);
req.on('data', function (chunk) {
console.log("req.use on data");
}).on('end', function () {
console.log("app.use on end");
});
next();
});
This is the call to the above;
$.ajax({ type:'post', url:'/api/order', headers: { 'GreatestHits': 'SteveMillerBand' }, data: { 'the': 'greatest' } });
Why isn't .on('data')
triggering? I am seeing the form data logging to the console.
Also. If the data segment is know as req.body
then why isn't it req.on('body')
?
Because, I find out, that Body-Parser.js
plete consumes the response
object it receives from Node.js
& it re-writes it thereby re-creating response
as an object expectionally different from the node.js
response
object.