Node.JS Redis connect EADDRNOTAVAIL error in Microsoft Azure Web App - node.js

I have a Node.js app deployed to a Microsoft Azure Web App and I can't seem to fix the following error:
Application has thrown an uncaught exception and is terminated:
> Error: Redis connection to
> <insertRedisHostNameHere> failed -
> connect EADDRNOTAVAIL
> at errnoException (net.js:670:11)
> at connect (net.js:548:19)
> at Socket.connect (net.js:613:5)
> at Object.<anonymous> (net.js:77:12)
> at RedisClient.create_stream (D:\home\site\wwwroot\node_modules\redis\index.js:218:31)
> at new RedisClient (D:\home\site\wwwroot\node_modules\redis\index.js:169:10)
> at Object.createClient (D:\home\site\wwwroot\node_modules\redis\index.js:906:12)
> at Object.<anonymous> (D:\home\site\wwwroot\server.js:8:20)
> at Module._compile (module.js:446:26)
> at Object..js (module.js:464:10)
The code that is causing this error is:
var redis = require('redis');
var client = redis.createClient(17397, '<hostname>', {enable_offline_queue: false});
client.auth('<password>', function(err){
});
client.on('connect', function(){
console.log('connected to redis');
});
I haven't been able to find a working solution, any help is greatly appreciated.
Additionally, I am able to run the same code on my localhost and local Redis cache without any problems at all.

Fixed it by updating the version of Node JS in package.json. (Refer to this question for more details: can't connect to DB - EADDRNOTAVAIL)

Related

Error: No event 'socketConnect' in state 'SentPrelogin'

I am trying to connect my SQL Server database with node.js using knex but I am facing issue
Error: No event 'socketConnect' in state 'SentPrelogin'
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:26)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
at C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1145:12
at Socket.onConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connector.js:106:7)
at Socket.emit (events.js:314:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
Emitted 'error' event on Connection instance at:
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:12)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
[... lines matching original stack trace ...]
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
[nodemon] app crashed - waiting for file changes before starting...
My code is
var knex = require('knex')({
client: 'mssql',
version:"7_1",
connection: {
user: 'sa',
password: 'Admin#123',
server: 'localhost',
database: 'Demo'
}
});
knex.select("*").from("Country")
.then(function (depts){
depts.forEach((dept)=>{ //use of Arrow Function
console.log({...dept});
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});
You need to add a missing dependency:
npm install --save tedious
As of knex v0.95.0 you'll need to use the tedious library instead of mssql when connecting to an MSSQL database. According to the knex upgrade instructions:
MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses tedious library directly instead of mssql, please replace mssql with tedious in your dependencies if you are using a MSSQL database.
Installing the package above should resolve your issue. I also had to set the encrypt option to false when connecting to my local database to avoid this error:
ConnectionError: Failed to connect to localhost:1433 - self signed certificate

Unable to run even basic Node.Js file on raspberry pi

So I have installed node.js onto my raspberry pi. Checked that it was installed by running:
node -v
which yielded: 4.2.1 and
npm -v
which yielded 2.14.7. however when I try to run any .js file (that I have tested running in node on my pc) it fails with the following:
pi#raspberrypi:~/share/Node $ node Brew-View.js
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at EventEmitter.listen (/home/pi/share/Node/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/pi/share/Node/Brew-View.js:39:8)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
I even went back to basics and just ran a file that just had
console.log('Hello.');
in it and got the same result. This sounds like it is an issue being caused by a bad preinstalled module. but it is a fresh installation(both of node and raspbian). is there some setup step that you have to do specifically for node on pi or call it differently?
Note: this is on a pi 3 B+
Edit: this is the code I am running which is yielding the 141 error(I have added ip and express to that directory via npm):
const express= require('express')
const ip = require('ip')
const ipAddr=ip.address();
const port=80;
const Server= express();
Server.use(express.static('public'))
Server.get('/',function(req,res){
res.send('<html>'+
'<head>'+
'<meta charset ="UTF-8">'+
'<link rel="stylesheet" type="text/css" href="./main.css"></head><body>'+
'<h1>Brew View</h1>'+
'<p>my IP is: '+ipAddr+":"+port+'</p>'+
'</body>'+
'</html>')
})
// Handle 404
Server.use(function(req, res) {
res.status(404).send('<html>'+
'<head>'+
'<meta charset ="UTF-8">'+
'<link rel="stylesheet" type="text/css" href="./main.css"></head><body>'+
'<h1>404: Page not Found</h1>'+
'</body>'+
'</html>');
});
// Handle 500
Server.use(function(error, req, res, next) {
res.send('500: Internal Server Error', 500);
});
Server.use(express.static('public'))
Server.listen(80,()=> console.log("Hello. Brew View is Hosted at: "+ipAddr+":"+port))
this was also occurring with a more basic http module based server as well.
Edit: so The solution was that you can't automatically host on port 80 without root.

I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.
(I had to remove some details to post the question)
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at Server.setupListenHandle [as _listen2] (net.js:1305:19)
at listenInCluster (net.js:1370:12)
at Server.listen (net.js:1466:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:9:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TFP#1.0.0 start: `node index.js`
npm ERR! Failed at the TFP#1.0.0 start script.
Problem
I had the same issue. I ran into this problem when I tried to set the port manually:
app.listen(80, function () {
console.log('Example blog app listening on port 80!')
});
This led me to the same EACCES error.
Solution
I was able to solve the problem by going back and looking through the Heroku docs where I found:
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
So, assuming you're using express, if you switch the port assignment to look something like this:
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Then you application should start properly.
Solution without Express.js
If you are not using Express, then you can set the port simply as:
var PORT = process.env.PORT || 5000;
Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.
I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port.
Now you can run your app without this error.
Thanks

Node & Redis Application Error on Heroku

I am new on Heroku & Node so please bear with me. I deployed my working chatting app(on localhost) to Heroku but can't open the Heroku app.
Can anyone help me how to fix this issue?
I looked at some similar questions on stackoverflow and did heroku config:add REDISCLOUD_URL='redis_cloud_url' and heroku config:add NODE_ENV='production' but those didn't work.
Here is my heroku log.
2015-04-11T19:42:35.568690+00:00 heroku[api]: Deploy 0244dd7 by MYMAIL#gmail.com
2015-04-11T19:42:38.004491+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-04-11T19:42:39.182617+00:00 app[web.1]: Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-04-11T19:42:39.182639+00:00 app[web.1]: Recommending WEB_CONCURRENCY=1
2015-04-11T19:42:39.793252+00:00 app[web.1]: events.js:85
2015-04-11T19:42:39.793258+00:00 app[web.1]: throw er; // Unhandled 'error' event
2015-04-11T19:42:39.793260+00:00 app[web.1]: ^
2015-04-11T19:42:39.793262+00:00 app[web.1]: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
2015-04-11T19:42:39.793264+00:00 app[web.1]: at RedisClient.on_error (/app/node_modules/redis/index.js:196:24)
2015-04-11T19:42:39.793266+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/redis/index.js:106:14)
2015-04-11T19:42:39.793268+00:00 app[web.1]: at Socket.emit (events.js:107:17)
2015-04-11T19:42:39.793270+00:00 app[web.1]: at net.js:459:14
2015-04-11T19:42:39.793271+00:00 app[web.1]: at process._tickCallback (node.js:355:11)
2015-04-11T19:42:40.546881+00:00 heroku[web.1]: State changed from starting to crashed
Okay I know the problem actually, the issue is that you're using redis cloud, so redis is not running on 127.0.0.1
Have a look at the official documentation on Heroku; you must not set REDISCLOUD_URL to redis_cloud_url, you must let Heroku set it to your account's REDISCLOUD_URL, that you can get by doing this in your terminal
heroku config:get REDISCLOUD_URL
It should return something like this ;
http://rediscloud:password#hostname:port
If it returns something without this format then the addon is not properly configured / activated for your app.
And your code to connect to redis should look like this :
var redis = require('redis');
var url = require('url');
var redisURL = url.parse(process.env.REDISCLOUD_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);
I use Heroku Redis, not Redis To Go and also had such an issue. However I solved the issue by changing the way Redis client is initialized, previously I had following code:
const
redisOptions = require('./redisOptions'),
redis = require('redis'),
redisClient = redis.createClient(redisOptions);
now I removed url parameter from redisOptions and then add it to createClient method, i.e.:
const
redisOptions = require('./redisOptions'),
redis = require('redis'),
redisClient = redis.createClient(REDIS_URL, redisOptions);
then the error was fixed. Really not sure why it is working with no issues on my local environment but I need this fix on Heroku.

Connection bug with socket.io-client (Server to Server)

I have a problem with socket.io-client normaly when i connect my clientWS to the ServerWS it try to connect in loop even if the ServerWS is not started.
I tried it in a simple node app and it work, but when I'm trying to include it in another project i got this error:
.../node_modules/socket.io-client/lib/manager.js:75
this.nsps[nsp].emit.apply(this.nsps[nsp], arguments);
^
TypeError: Cannot call method 'apply' of undefined
at Manager.emitAll (.../node_modules/socket.io-client/lib/manager.js:75:25)
at null._onTimeout (.../node_modules/socket.io-client/lib/manager.js:464:12)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
16 Mar 21:54:03 - [nodemon] app crashed - waiting for file changes before starting...
And it's the same connection code in both project :
var socket = require("socket.io-client");
var ws = socket("http://localhost:3000");
So I don't know what is the problem...
(tested with socket.io and socket.io-client 1.3.5)

Resources