【发布时间】:2017-03-01 13:15:13
【问题描述】:
我想使用带有 express 的 node.js 创建一个服务器端应用程序。目前,我能够获取包含位置和温度的 json 文档,但是在我使用 pykite 使其在线后,应用程序只会将服务器位置提供给正在访问它的人。以下是当前形式的代码:
var express = require('express');
var app = express();
var request = require('request');
app.get('/api/weather/current_temperature', function (req, res) {
request('http://api.wunderground.com/api/ebb3e0cf5059cce8/conditions/q/autoip.json', function(error, response, body){
if (!error && response.statusCode==200){
res.setHeader('Content-Type', 'aplication/json');
var result = JSON.parse(body);
res.json({ "location": result.current_observation.observation_location.city,"temperature": result.current_observation.temp_c });
}else{
console.log(error, response.statusCode, body);
}
res.end("")
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('The app is up.');
});
我承认我对此很陌生,但我想学习。如果可能的话,我希望在此代码中实现它的一些帮助,一种访问用户位置并确定他/她位置的天气的方法。 提前谢谢你。
P.S.:我还想要一些文档的链接,不想跳过任何内容。
【问题讨论】:
-
一个简单的谷歌搜索会给你结果。 stackoverflow.com/questions/27234861/…
标签: javascript node.js express