javascript - Node Restify 获取数据的用例给出了 "ResourceNotFound"

标签 javascript node.js restify

我刚刚开始使用 Nodejs。

我正在使用 Restify 从以下位置获取数据:http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo '.

下面的代码给了我一个错误:{“code”:“ResourceNotFound”,“message”:“/不存在”}

var restify =require("restify");

var server = restify.createServer();

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.get('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo', function (req, res) {
    console.log(req.body);
    res.send(200,req.body);
});

server.listen(7000, function () {
    console.log('listening at 7000');
});

最佳答案

这是因为 Restify 用于创建 REST 端点,而不是使用它们。您应该查看this SO post获取使用 API 数据的帮助。

例如使用以下内容创建 test.js:

var http = require('http');
var options = {
  host: 'api.geonames.org',
  path: '/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'
};

var req = http.get(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));

  // Buffer the body entirely for processing as a whole.
  var bodyChunks = [];
  res.on('data', function(chunk) {
    // You can process streamed parts here...
    bodyChunks.push(chunk);
  }).on('end', function() {
    var body = Buffer.concat(bodyChunks);
    console.log('BODY: ' + body);
    // ...and/or process the entire body here.
  })
});

req.on('error', function(e) {
  console.log('ERROR: ' + e.message);
});

然后运行node test.js

关于javascript - Node Restify 获取数据的用例给出了 "ResourceNotFound",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513237/

相关文章:

javascript - 如何使用 Node 一次安装多个 gulp 包?

javascript - 使用 Node.js 和 restify 进行基本身份验证

javascript - Node JS .shift() 'ing 一个由 const 初始化的数组

node.js - 通过语音 channel 播放音频的问题;音频被截断并且每个文件逐渐延迟

javascript - 单击链接到它的轮播幻灯片时更改按钮的颜色

javascript - 为什么这个 typescript 程序永远不会结束?

json - Express.Js 可以输出缩小的 JSON 吗?

node.js - 如何获取我在 Restify 服务器中使用的所有路由的列表

javascript - 使用循环运行 Gulp 任务

javascript - ExtJS 是否自动垃圾收集组件