NodeJS HttpGet转换为带有JSON响应的URL

我是新的节点开发,我试图做一个服务器端API调用使用RESTful协议与JSON响应。 我已经阅读了API文档和这个SOpost 。

我试图拉的API跟踪总线并返回JSON输出中的数据。 我很困惑如何使用实际URL中的所有参数和选项进行HTTP GET请求。 API和它的响应甚至可以通过浏览器或使用“curl”命令来访问。 http://developer.cumtd.com/api/v2.2/json/GetStop?key=d99803c970a04223998cabd90a741633&stop_id=it

如何编写Node服务器端代码,向URL中的选项发出GET请求,并解释JSON响应?

提前致谢!

请求模块使这非常简单。 将请求从npm安装到你的包中,然后你可以发出一个请求。

var request = require("request") var url = "http://developer.cumtd.com/api/v2.2/json/GetStop?" + "key=d99803c970a04223998cabd90a741633" + "&stop_id=it" request({ url: url, json: true }, function (error, response, body) { if (!error && response.statusCode === 200) { console.log(body) // Print the json response } }) 

你可以在npm上find请求的文档: https ://npmjs.org/package/request