Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 错误:getaddrinfo ENTFOUND在nodejs中进行get调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_616_4@ 我在节点上运行一个Web服务器,代码如下

var restify = require('restify');

var server = restify.createServer();

var quotes = [
  { author : 'Audrey Hepburn',text : "Nothing is impossible,the word itself says 'I'm possible'!"},{ author : 'Walt Disney',text : "You may not realize it when it happens,but a kick in the teeth may be the best thing in the world for you"},{ author : 'UnkNown',text : "Even the greatest was once a beginner. Don't be afraid to take that first step."},{ author : 'Neale Donald Walsch',text : "You are afraid to die,and you're afraid to live. what a way to exist."}
];

server.get('/',function(req,res) {
  res.json(quotes);
});

server.get('/quote/random',res) {
  var id = Math.floor(Math.random() * quotes.length);
  var q = quotes[id];
  res.json(q);
});

server.get('/quote/:id',res) {
  if(quotes.length <= req.params.id || req.params.id < 0) {
    res.statusCode = 404;
    return res.send('Error 404: No quote found');
  }

  var q = quotes[req.params.id];
  res.json(q);
});

server.listen(process.env.PORT || 3011);

然后我想在以下代码中做一个获取请求

var https = require('http');

/**
 * HOW TO Make an http Call - GET
 */
// options for get
var optionsget = {
    host : 'http://localhost',port : 3010,path : '/quote/random',// the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsget,function(res) {
    console.log("statusCode: ",res.statusCodE);
    // uncomment it for header details
//  console.log("headers: ",res.headers);


    res.on('data',function(d) {
        console.info('GET result:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error',function(E) {
    console.error(E);
});

我刚刚从节点开始,我甚至不知道这是否是正确的方式。@H_404_20@我想测试express和restify的性能。我已经对我编写的服务器代码进行了apache基准测试,发现重新排列的矛盾结果更好。所以我想通过调用远程服务再测试一些,然后再来读写mongodb。上面的代码是我的起点。我收到错误

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND',errno: 'ENOTFOUND',syscall: 'getaddrinfo' }

我至少在写方向吗?我想要的那种测试的正确方法是什么?为什么我的结果比快递更快?任何人都可以指导我在Node / express / BACkbone和mongodb中应用的最佳起点教程?

解决方法

getaddrinfo ENOTFOUND表示客户端无法连接到给定的地址。 @H_404_20@请尝试指定不带http的主机:

var optionsget = {
    host : 'localhost',// the rest of the url with parameters if needed
    method : 'GET' // do GET
};

关于学习资源,如果你从http://www.nodebeginner.org/开始,然后通过一些好书来获得更深入的知识,那么你不会出错 – 我推荐Professional Node.js,但有很多。

大佬总结

以上是大佬教程为你收集整理的node.js – 错误:getaddrinfo ENTFOUND在nodejs中进行get调用全部内容,希望文章能够帮你解决node.js – 错误:getaddrinfo ENTFOUND在nodejs中进行get调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。