【问题标题】:Node Restify use case to get data gives a "ResourceNotFound"Node Restify 获取数据的用例给出了“ResourceNotFound”
【发布时间】:2015-11-06 08:24:39
【问题描述】:

我刚开始使用 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');
});

【问题讨论】:

    标签: javascript node.js restify


    【解决方案1】:

    这是因为 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 新手既想使用 API 又想提供 API,那么最好检查一下 restify 中的内置客户端。 restify.com/#client-api
    【解决方案2】:

    我找到了我要找的东西。您可以使用restify客户端获取JSON数据:

    这是我的解决方案:

    var restify = require("restify");
    
     function getJSONDataFromUrl(){
        var query = "?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";
        var options = {};
        options.url = "http://api.geonames.org";
        options.type = options.type || "json";
        options.path = "/citiesJSON" + query;
        options.headers = {Accept: "application/json"};
    
    
        var client = restify.createClient(options);
    
        client.get(options, function(err, req, res, data) {
            if (err) {
                console.log(err);
                return;
            }
            client.close();
            console.log(JSON.stringify(data));
    
            return JSON.stringify(data);
        });
    }
    
    getJSONDataFromUrl();
    

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2016-02-05
      • 2016-08-25
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2019-08-11
      • 2020-05-29
      相关资源
      最近更新 更多