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

javascript - What does response.on mean? Node.js - Stack Overflow

programmeradmin3浏览0评论

I am having trouble with a Node.js http request. (A bigger question I will ask about later if I can't figure it out).

I have code that I modified from and an example and I don't understand what the response.on means. As read more about http in Node.js (Anatomy of an HTTP Transaction) I don't see people using examples with 'response.on'. I think I know but I want to clarify. Oh I am using Express.js too.

Thanks

Below is the code I am using to try and call the BART api I need a response from.

...
// Real Time Departure from a given station
router.route('/departTimeStation')
.get(function(req, res) {
    vCmd = 'etd';
    vOrig = req.query.vOriginStation;
    vDir = 'n'; // [NOTE] - 'n' or 's', north or south, OPTIONAL
    vPlat = 1; // [NOTE] - 1 to 4, number of platform, OPTIONAL

    var xoptions = {
        host: 'api.bart.gov',
        path: '/api/etd.aspx?cmd=' + vCmd + '&orig=' + vOrig + '&key=' + config.bart.client
    };

    var xcallback = function(response) {

        response.on('data', function(chunk) {
            vParsed += chunk;
        });

        response.on('end', function() {
            parseString(vParsed, function(err, result) {
                vShow = JSON.stringify(result);
                vTemp = result;
            });
        });
    };
    http.request(xoptions, xcallback).end();
    return res.send (vTemp)
});
...

I am having trouble with a Node.js http request. (A bigger question I will ask about later if I can't figure it out).

I have code that I modified from and an example and I don't understand what the response.on means. As read more about http in Node.js (Anatomy of an HTTP Transaction) I don't see people using examples with 'response.on'. I think I know but I want to clarify. Oh I am using Express.js too.

Thanks

Below is the code I am using to try and call the BART api I need a response from.

...
// Real Time Departure from a given station
router.route('/departTimeStation')
.get(function(req, res) {
    vCmd = 'etd';
    vOrig = req.query.vOriginStation;
    vDir = 'n'; // [NOTE] - 'n' or 's', north or south, OPTIONAL
    vPlat = 1; // [NOTE] - 1 to 4, number of platform, OPTIONAL

    var xoptions = {
        host: 'api.bart.gov',
        path: '/api/etd.aspx?cmd=' + vCmd + '&orig=' + vOrig + '&key=' + config.bart.client
    };

    var xcallback = function(response) {

        response.on('data', function(chunk) {
            vParsed += chunk;
        });

        response.on('end', function() {
            parseString(vParsed, function(err, result) {
                vShow = JSON.stringify(result);
                vTemp = result;
            });
        });
    };
    http.request(xoptions, xcallback).end();
    return res.send (vTemp)
});
...
Share Improve this question asked Apr 16, 2016 at 0:54 TrewatersTrewaters 1,0793 gold badges14 silver badges23 bronze badges 1
  • It's this – Fozi Commented Apr 16, 2016 at 0:58
Add a ment  | 

2 Answers 2

Reset to default 6

response.on can take a few 'events' that happen during the response life cycle,

The two you have above are response.on('data'...) which is when you get the data and response.on('end'...) is at the end of the response, simple as that!

Node Docs on response: https://nodejs/api/http.html#http_class_http_iningmessage

response.on is a function, or rather method, since it is a property.

发布评论

评论列表(0)

  1. 暂无评论