使用 nodejs 调用雅虎天气 api

call yahoo weather api with nodejs

我想实现以下功能。

当浏览器点击 localhost:3000/weather 服务器时,服务器会转到用 "weather" 装载路径定义的路由器。

为了从 yahoo weather api 获取数据,我应该在那个路由中用 superagent 调用它还是需要代理?

详细了解 requeston npm

先做

npm install request

然后,在你的路由文件中,说 index.js

router.get('/weather', function(req, res, next){
    request('http://www.yahoo.com/your/api/url', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
            res.send(body); // this will send data to client.
        }
    })
});