Node.js crashing - node.js

My node.js app keeps crashing. It's a simple web service that gets data from a mysql database. I get about 20k to 30k queries a day. I'm not sure if it's crashing because I need to give it more resources or if I have a problem with my code. It's hosted in AppFog and here's the crashlog.
I'm not sure what the hashish package is but I tried installing it using 'npm install hashish' and it didn't fix the problem.
Any thoughts?
C:\Users\Tom\\nodejs>af crashlogs TomsApp
====> /logs/staging.log <====
# Logfile created on 2013-02-05 00:53:01 +0000 by logger.rb/25413
Installing dependencies. Node version 0.8.14
Installing mysql#mysql#0.9.6 from local path
Installing hashish#hashish#0.0.4 from registry
Package is not found in npm registry hashish#hashish#0.0.4
Failed getting the requested package: hashish#hashish#0.0.4
Installing require-all#require-all#0.0.5 from local path
====> /logs/stderr.log <====
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/mnt/var/vcap.local/dea/apps/ta-0-365d7313671d4e40105a4d158
f1247d5/app/node_modules/mysql/lib/protocol/Protocol.js:63:13)
at Socket.onend (stream.js:66:10)
at Socket.EventEmitter.emit (events.js:126:20)
at TCP.onread (net.js:417:51)

How are you getting your connection? Do you get it once, or get a new one for each request and then return it to the pool? The pool should be handling stale connections behind the scene.
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
// for each request where you need the database connection, wrap it with
pool.getConnection(function(err, connection) {
// do something with connection here
...
connection.end();
});

I would say mysql server's refusing the connection maybe you want to look into that

You probably have to handle the error event on the socket (http://nodejs.org/api/net.html#net_event_error_1) for all your connections.
That will keep it from crashing and you should then see the actual problem.

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

Sailsjs: Server crash after using create blueprint with mongodb

I configured the connections.js file in config. I used shortcut route to check whether data is getting stored in the database.
I put the url localhost:1337/test/create?name=hello
It creates an entry in the database but server crashes with this log
process.nextTick(function() { throw err; });
^
TypeError: Right-hand side of 'instanceof' is not callable
at _hasBinary (/usr/local/lib/node_modules/sails/node_modules/has-binary/index.js:31:30)
at hasBinary (/usr/local/lib/node_modules/sails/node_modules/has-binary/index.js:58:10)
at Namespace.emit (/usr/local/lib/node_modules/sails/node_modules/socket.io/lib/namespace.js:214:9)
at Object.broadcastToRoom [as broadcast] (/usr/local/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/sails.sockets/broadcast-to-room.js:74:13)
at child.broadcast (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:139:23)
at child.publishCreateSingle (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1218:14)
at /usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1069:16
at arrayEach (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:1439:13)
at Function.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:3500:13)
at child.publishCreate (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1068:11)
at child.wrapper [as publishCreate] (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:3250:19)
at created (/usr/local/lib/node_modules/sails/lib/hooks/blueprints/actions/create.js:48:19)
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
at success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:248:9
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:269:32
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at child.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:152:44)
Update:20-Dec-2017..
This is what i had gotten when in installed the >sails-mongo adapter...What exactly is the version 2.1.20 of..?
Faizaans-MacBook-Pro:foobar faiz$ npm install sails-mongo --save
npm WARN deprecated mongodb#2.1.20: Please upgrade to 2.2.19 or higher
+ sails-mongo#0.12.2
added 14 packages in 7.967s
I couldn't figure out the exact issue here. I tried to connect my project to mysql and the same issue came up. The problem got solved when I created a new project and tried connecting it to the database.

MongoLab - How to resolve ECONNREFUSED

I have a NodeJS application using MongoDB as the database. I'm using mongoose for mongodb related activities.
I have an account in MongoLab and I use that for my database related activities of my application.
When trying to connect to MongoLab as below from nodejs
mongoose.connect("<db_url_with_username_password>", function(err) {
if(err)
{
console.log("Error is "+err);
throw err;
}
});
It's logging the following line
Error is MongoError: connect ECONNREFUSED 54.161.65.217:19058
I thought the server could be down, hence I checked the website online, it's working perfectly.
I pinged the server with the following
ping 54.161.65.217
and it's working fine.
64 bytes from 54.161.65.217: icmp_seq=0 ttl=28 time=271.396 ms
64 bytes from 54.161.65.217: icmp_seq=1 ttl=28 time=268.824 ms
However, When I'm trying to connect using the mongo command to connect, it's failing
mongo 54.161.65.217:19058/<db_name> -u <username> -p <password>
MongoDB shell version: 3.2.1
connecting to: 54.161.65.217:19058/arogyavath
2016-03-14T10:28:27.200+0530 W NETWORK [thread1] Failed to connect to 54.161.65.217:19058, reason: errno:61 Connection refused
2016-03-14T10:28:27.201+0530 E QUERY [thread1] Error: couldn't connect to server 54.161.65.217:19058, connection attempt failed :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
I looked around and found the following questions which could be similar to mine
Node.js Error: connect ECONNREFUSED
ECONNREFUSED error when connecting to mongodb from node.js
But the answers I found there didn't help me fix the issue.
Could the server ip address be blocked from my network or something? How do I fix this?
I would be very glad if someone can point me in the right direction. Thanks in Advance.

Ghost Blog - Unable to connect with mysql

After deploying the Ghost blogging platform with success, i tried to configure it to use Mysql instead of sqllite3 using this database section of their config page, which says:
Database
By default, Ghost comes configured to use an SQLite database, which
requires no configuration.
Alternatively Ghost can also be used with a MySQL database by changing
the database configuration. You must create a database and user first,
you can then change the existing sqlite config to something like:
database: { client: 'mysql', connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'ghost_db',
charset : 'utf8' }
}
So ok, the setup is straight forward. but i'm still unable to connect ghost with mysql. The error i receive after starting the platform using npm start --production is :
> ghost#0.6.2 start /var/www/ghost
> node index
Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Creating table: posts
ERROR: connect ECONNREFUSED
Error: connect ECONNREFUSED
at errnoException (net.js:905:11)
at Object.afterConnect [as oncomplete] (net.js:896:19)
--------------------
at Protocol._enqueue (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:110:48)
at Protocol.handshake (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/ghost/node_modules/mysql/lib/Connection.js:98:18)
at /var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:105:16
at tryCatch2 (/var/www/ghost/node_modules/bluebird/js/main/util.js:53:21)
at Promise._resolveFromResolver (/var/www/ghost/node_modules/bluebird/js/main/promise.js:544:13)
at new Promise (/var/www/ghost/node_modules/bluebird/js/main/promise.js:84:37)
at Client_MySQL.acquireRawConnection (/var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:104:10)
at Object.create (/var/www/ghost/node_modules/knex/lib/pool.js:33:19)
at Object.Pool.createResource (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:288:12)
I'm not sure what could be wrong, since i have other applications using mysql working without any problems.
Thanks in advance.
connection refused = tcp connection was attempted, but nothing is listening on the port, or was explicitly denied. Unless you explicitly enabled TCP support in mysql (and have the correct ip/port), you should probably be using a local unix-domain socket instead.

Openshift redis cartridge with strange exceptions ETIMEDOUT on socket.io script

I have a socket.io node script running that acts as a chat server. I had it running on a local server ok, and have since tried moving it over to openshift.
I used the following cartridge to get it working - https://github.com/smarterclayton/openshift-redis-cart/issues
While running the local version of the server, while connecting to the openshift redis, I get strange timeouts exactly 127 seconds after I run the script.
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to <server> failed - connect ETIMEDOUT
at RedisClient.on_error (/path/node_modules/redis/index.js:196:24)
at Socket.<anonymous> (/path/node_modules/redis/index.js:106:14)
at Socket.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:442:13)
I have seen the events.js:72 one before, but its usually when I try to run a node script with something already running on that port.
Pretty sure the script itself is fine, as when I swap out the redis server for my local one, it will sit there for days and work fine.
So my question: why is my socket.io script killing itself after exactly 127 seconds when redis is hosted on openshift?
Are you using the rhc port-forward command so that you can connect to redis on OpenShift? Otherwise that port is not publicly available.

Resources