我正在节点上运行 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', function(req, res) {
var id = Math.floor(Math.random() * quotes.length);
var q = quotes[id];
res.json(q);
});
server.get('/quote/:id', function(req, 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);
});
我只是从节点开始,我什至不知道这是否是正确的方法。我想测试 express 和 restify 的性能。我对我编写的服务器代码进行了 apache 基准测试,发现restify更好的结果相互矛盾。所以我想通过调用远程服务然后稍后再测试一些读写mongodb。上面的代码是我的起点。我收到错误
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
我是否至少朝着写作方向前进?我想要的那种测试的正确方法是什么?为什么我得到的结果比快递快?谁能指导我在 node/express/backbone 和 mongodb 中的应用程序的最佳起点教程?
process.stdout.alwrite.now(d)
getaddrinfo ENOTFOUND 表示客户端无法连接到给定地址。请尝试指定不带 http 的主机:
var optionsget = {
host : 'localhost',
port : 3010,
path : '/quote/random', // the rest of the url with parameters if needed
method : 'GET' // do GET
};
关于学习资源,如果你从 http://www.nodebeginner.org/ 开始,然后通过一些好书获得更深入的知识,你不会出错 - 我推荐 Professional Node.js,但那里有很多。
检查像这样的主机文件
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
/etc/hosts
文件是空的!在mac上更改文件后,运行sudo killall -HUP mDNSResponder
使更改生效!
对我来说,这是因为在 /etc/hosts 文件中没有添加主机名
我无法解释原因,但是在 Windows 10 上的 mongodb.MongoClient.connect()
方法上将 url 参数从 localhost
更改为 127.0.0.1
解决了这个问题。
我也有同样的问题,我通过使用正确的代理解决了它。如果您使用代理网络,请仔细检查您的代理设置。
希望对你有帮助 -
我对亚马逊服务器有同样的问题我将我的代码更改为此
var connection = mysql.createConnection({
localAddress : '35.160.300.66',
user : 'root',
password : 'root',
database : 'rootdb',
});
检查 mysql 节点模块 https://github.com/mysqljs/mysql
我的问题是我的端点是 http://tenant1.localhost
。虽然这以某种方式在浏览器中有效,但它不适用于节点。
我必须将以下行添加到 /etc/hosts
127.0.0.1 tenant1.localhost
var http = require('http');
var options = {
host: 'localhost',
port: 80,
path: '/broadcast'
};
var requestLoop = setInterval(function(){
http.get (options, function (resp) {
resp.on('data', function (d) {
console.log ('data!', d.toString());
});
resp.on('end', function (d) {
console.log ('Finished !');
});
}).on('error', function (e) {
console.log ('error:', e);
});
}, 10000);
var dns = require('dns'), cache = {};
dns._lookup = dns.lookup;
dns.lookup = function(domain, family, done) {
if (!done) {
done = family;
family = null;
}
var key = domain+family;
if (key in cache) {
var ip = cache[key],
ipv = ip.indexOf('.') !== -1 ? 4 : 6;
return process.nextTick(function() {
done(null, ip, ipv);
});
}
dns._lookup(domain, family, function(err, ip, ipv) {
if (err) return done(err);
cache[key] = ip;
done(null, ip, ipv);
});
};
// Works fine (100%)
当我尝试安装一个新的 ionic 应用程序时,我得到了以下相同的错误,我尝试了很多来源,发现用户环境和系统环境中的错误不必要地包含了 PROXY 值。我删除了```用户变量 http://host:port PROXY
系统变量 http_proxy http://username:password@host:port ```` 现在它可以正常工作了。
[ERROR] Network connectivity error occurred, are you offline?
If you are behind a firewall and need to configure proxy settings, see: https://ion.link/cli-proxy-docs
Error: getaddrinfo ENOTFOUND host host:80
我有一个类似的问题,结果问题出在我的代理密码上。
错误是:
“启动 npm npm ERR!code ENOTFOUND npm ERR!errno ENOTFOUND npm ERR!对 https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz 的网络请求失败,原因:getaddrinfo ENOTFOUND myUsername npm ERR! network 这是与网络连接有关的问题。npm ERR!network 在大多数情况下,您使用代理或网络设置错误。npm ERR!network npm ERR!network 如果您使用代理,请确保 npm ERR ! 网络“代理”配置设置正确。请参阅:“npm 帮助配置”
原来有趣的部分是这个:
失败,原因:getaddrinfo ENOTFOUND myUsername
我在代理中使用的密码具有特殊字符,例如!和 #。在我使用百分比编码后:https://en.wikipedia.org/wiki/Percent-encoding 我能够运行 npm install。在此之前,我尝试使用错误的密码并收到 407 错误。
我遇到了同样的问题,经过反复试验发现我的 hosts
文件自定义导致了问题。
我的 localhost
DNS 被覆盖,因此简单的 ping 失败:
$ ping http://localhost
# ping: cannot resolve http://localhost: Unknown host
一旦我解决了 /private/etc/hosts
文件的配置,ping localhost
就成功了,并且不再收到您所看到的错误:
# Fatal error: getaddrinfo ENOTFOUND
不定期副业成功案例分享
getaddrinfo ENOUTFOUND means client was not able to connect...
->确保在使用终端或提示 ping 该地址时得到回复。localhost
作为主机但它仍然无法工作,请检查您的\etc\hosts
文件是否有以下行:127.0.0.1 localhost
。