2013-11-04 164 views
1

我有些新的Node.js,寻找一些见解如何,我可以利用正被另一个人的API提供的信息。例如。股市API的node.js

继承人我想API使用 http://live-nse.herokuapp.com/?symbol=AMAR 的源代码可以在https://github.com/ashwanthkumar/Live-NSE-Stock

林在我如何使用这些信息,例如,如果您使用的链接很感兴趣被发现为了获得符号AMAR的统计数据,我相信它会用JSON做出响应? (如果我错了,请纠正我)。

下面是一个例子响应它给出。

{"lastUpdateTime":"03-NOV-2013 19:50:03","tradedDate":"03NOV2013","data":[{"extremeLossMargin":"-","cm_ffm":"15.47","bcStartDate":"19-DEC-12","change":"0.35","buyQuantity3":"200","sellPrice1":"-","buyQuantity4":"181","sellPrice2":"-","priceBand":"5","buyQuantity1":"530","deliveryQuantity":"-","buyQuantity2":"1","cm_adj_low":"6.00","sellPrice5":"-","quantityTraded":"-","buyQuantity5":"1,000","sellPrice3":"-","sellPrice4":"-","open":"7.55","cm_adj_high":"48.20","low52":"6.00","securityVar":"-","marketType":"N","pricebandupper":"8.25","totalTradedValue":"0.11","faceValue":"10.00","ndStartDate":"-","previousClose":"7.55","symbol":"AMAR","varMargin":"100.00","lastPrice":"7.90","pChange":"4.64","adhocMargin":"-","companyName":"Amar Remedies Limited","averagePrice":"7.78","secDate":"-","series":"BE","isinCode":"INE787G01011","indexVar":"-","pricebandlower":"7.55","totalBuyQuantity":"2,113","high52":"48.20","purpose":"ANNUAL GENERAL MEETING","cm_adj_low_dt":"28-JUN-13","closePrice":"7.90","recordDate":"-","cm_adj_high_dt":"08-JAN-13","totalSellQuantity":"-","dayHigh":"7.90","exDate":"17-DEC-12","sellQuantity5":"-","bcEndDate":"26-DEC-12","ndEndDate":"-","sellQuantity2":"-","sellQuantity1":"-","buyPrice1":"7.85","sellQuantity4":"-","buyPrice2":"7.40","sellQuantity3":"-","applicableMargin":"100.00","buyPrice4":"7.30","buyPrice3":"7.35","buyPrice5":"7.25","dayLow":"7.55","deliveryToTradedQuantity":"-","totalTradedVolume":"1,352"}]} 

我想知道如何让我的Node.JS应用程序接收此信息。我可以将其设置为VAR,所以我可以在后面的任何地方引用它?

回答

2

您可以创建这样一个请求:

var http = require('http'); 

var options = { 
    host: 'live-nse.herokuapp.com', 
    port: 80, 
    path: '/?symbol=AMAR' 
}; 

http.get(options, function(res){ 
    res.setEncoding('utf8'); 
    var data=""; 
    res.on('data', function(chunk){ 
    data += chunk; 
    }); 
    res.on('end', function(){ 
    var obj = JSON.parse(data); 
    //do whatever with obj 
    }); 
});