最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js ~ express req.on('end') triggers but not req.on('data') - Stack Overflow

programmeradmin0浏览0评论

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() {} )

  • has req.body
  • does not have req.data (undefined)
    • will NOT trigger .on('data');
    • will trigger .on('end');
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
  • 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's mount. – 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, so data will never trigger for you (because there won't be any more data to read). Also: show how you configured body-parser. – robertklep Commented Mar 18, 2017 at 21:27
 |  Show 10 more ments

1 Answer 1

Reset to default 5

The 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.

发布评论

评论列表(0)

  1. 暂无评论