Reputation: 1425
I am running a web server on node the code for which is given below
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);
And then i want to do a get request in the following code
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);
});
I am just starting with node and i dont even know if this is the right way. I want to test the performance of express and restify.I have done a apache benchmark test for the server code i wrote and found contradicting results that restify is better.So i want to test out some more by making calls to remote services and then later read write to mongodb.The above code is my starting point.I am getting the error
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
Am i atleast heading in the write direction? What is the right way to the kind of tests that i want to? Why did i get the result restify faster than express? Can anyone guide me to the best starting point tutorials for application in node/express/backbone and mongodb?
Upvotes: 91
Views: 470491
Reputation: 49
I had getaddrinfo ENOTFOUND localhost:3000 error. Could not connect to my database. My issue was that I've created 2 different databases in one postgresql db server. All of my db server properties were set correctly. For example: host: localhost port: 5432 username: myUsername password: xxxxxx
Only when I checked "Connection String" in my db server properties it had different dbname. Not the one I were expected it to pick up on. Just double check properties "connection string" as well in your database server properties.
Upvotes: 0
Reputation: 72
I had a similar issue and it turned out the issue was with my proxy password.
The error was:
"Start npm npm ERR! code ENOTFOUND npm ERR! errno ENOTFOUND npm ERR! network request to https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz failed, reason: getaddrinfo ENOTFOUND myUsername npm ERR! network This is a problem related to network connectivity. npm ERR! network In most cases you are behind a proxy or have bad network settings. npm ERR! network npm ERR! network If you are behind a proxy, please make sure that the npm ERR! network 'proxy' config is set properly. See: 'npm help config'
It turned out the interesting part was this one :
failed, reason: getaddrinfo ENOTFOUND myUsername
My password, that was used in the proxy, had special characters like ! and #. After I used percent encoding : https://en.wikipedia.org/wiki/Percent-encoding I was able to run npm install. Before this I tried with a wrong password and a received a 407 error.
Upvotes: 0
Reputation: 2582
My problem was that my endpoint was http://tenant1.localhost
. While this somehow works in the browser, it did not with node.
I had to add the following line to /etc/hosts
127.0.0.1 tenant1.localhost
Upvotes: 6
Reputation: 231
When I tried to install a new ionic app, I got the same error as follows, I tried many sources and found the mistake made in User Environment and System Environment unnecessarily included the PROXY value. I removed the ```user variables http://host:port PROXY
system Variables http_proxy http://username:password@host:port ```` and now it is working fine without trouble.
[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
Upvotes: 0
Reputation: 991
Check host file which like this
##
# 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
Upvotes: 34
Reputation: 179
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%)
Upvotes: 0
Reputation: 60074
i have same issue with Amazon server i change my code to this
var connection = mysql.createConnection({
localAddress : '35.160.300.66',
user : 'root',
password : 'root',
database : 'rootdb',
});
check mysql node module https://github.com/mysqljs/mysql
Upvotes: 1
Reputation: 13767
I can't explain why, but changing url parameter from having localhost
to 127.0.0.1
on mongodb.MongoClient.connect()
method on Windows 10 solved the issue.
Upvotes: 9
Reputation: 1271
I also had same problem and I fixed it by using right proxy. Please double check your proxy settings if you are using proxy network.
Hope this will help you -
Upvotes: 2
Reputation: 397
for me it was because in /etc/hosts file the hostname is not added
Upvotes: 18
Reputation: 2674
I was running into the same problem and after some trial and error discovered that my hosts
file customisation was causing the problem.
My localhost
DNS was overwritten and because of that a simple ping was failing:
$ ping http://localhost
# ping: cannot resolve http://localhost: Unknown host
Once I resolved the configuration of /private/etc/hosts
file, pinging localhost
successfully worked, and was no longer getting the error that you were seeing:
# Fatal error: getaddrinfo ENOTFOUND
Upvotes: -3
Reputation: 1436
getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without http:
var optionsget = {
host : 'localhost',
port : 3010,
path : '/quote/random', // the rest of the url with parameters if needed
method : 'GET' // do GET
};
Regarding learning resources, you won't go wrong if you start with http://www.nodebeginner.org/ and then go through some good book to get more in-depth knowledge - I recommend Professional Node.js , but there's many out there.
Upvotes: 135