Nodejs Service crashed and exit after few hours [duplicate] - mysql

I am trying to solve this issue for last 10 days. I tried everything I found on google.
I search and tried with many way but did not get any appropriate solution.
After running "npm start" at server at night I found this error at morning.
throw er; // Unhandled 'error' event
^
Error: Packets out of order. Got: 0 Expected: 244
at Parser.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Parser.js:42:19)
at Protocol.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (D:\people_hub\backend\node_modules\mysql\lib\Connection.js:103:28)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (D:\people_hub\backend\node_modules\mysql\lib\Connection.js:433:8)
at Protocol.emit (events.js:314:20)
at Protocol._delegateError (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:392:10)
at Protocol.handleParserError (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:374:10)
at Parser.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Parser.js:50:14)
at Protocol.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:39:16)
[... lines matching original stack trace ...]
at readableAddChunk (_stream_readable.js:272:9) {
code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
fatal: true
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! hrproject#1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hrproject#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\era\AppData\Roaming\npm-cache\_logs\2021-11-11T12_13_12_697Z-debug.log
My Sql connection file:
const Sequelize = require('sequelize')
const dotenv = require('dotenv');
dotenv.config();
const sequelize =
new Sequelize(process.env.DBNAME, process.env.DBUSER, process.env.DBPASS,
{
host: process.env.HOST,
port: process.env.HOST_PORT,
dialect: 'mysql',
operatorsAliases: 0, //false,
timezone: "+06:00",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
}
}
)
module.exports = sequelize;
My app.js file:
const express = require('express');
var cookieParser = require("cookie-parser");
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: false /*, parameterLimit: 50000*/ }));
app.use(cookieParser());
const Login = require('./routes/mysql-login');
app.use('', Login);
module.exports = app;
My Final Connection File:
var app = require("../server");
var debug = require("debug") // ("rest-api-nodejs-mongodb:server");
const db = require("../database/db");
var http = require("http");
const fs = require('fs');
var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
var server = http.createServer(app);
setConnection();
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
function setConnection() {
db.sync()
.then((result) => {
if (result) {
console.log("DB Connected");
console.log(`Nodejs Server started on port: ` + port);
}
})
.catch((err) => {
console.log("Database Connection Error: ", err);
})
};
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== "listen") {
console.error(`MySQL Connection Error: ${error}`);
throw error;
}
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
case "ELIFECYCLE":
console.error("MySQL Connection Lost");
setConnection();
default:
console.error(`MySQL Database Error: ${error}`);
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
I modified the mysql database with:
set global net_buffer_length=1000000;
set global max_allowed_packet=1000000000;
Everything I found on google I tried. but Failed at the end. So what to do now?

Updating "sequelize" and "mysql2" npm packages at Nodejs end with latest version solved the problem for now.

Related

Github node action http errors always fails job

I have a github action that looks something like this:
const http = require('http');
const core = require('#actions/core');
const options = {
hostname: 'mydomain.com',
port: 80,
path: '/webhook',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
};
const postData = {
foo: 'bar'
}
try {
const strPostData = querystring.stringify(postData);
options.headers['Content-Length'] = Buffer.byteLength(strPostData);
const req = http.request(options)
req.write(strPostData)
req.end()
} catch (error) {
core.info(error.message);
}
if mydomain.com/webhook is offline, my job fails on this action, logging:
events.js:187
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED xxx.xxx.xxx.xxx:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (_http_client.js:406:9)
at Socket.emit (events.js:210:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) ***
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: 'xxx.xxx.xxx.xxx',
port: 80
***
Why does it fail, why doesn't the try catch catch this?
I don't want the entire job to fail if this action fails, so how do I trap this failure and hide it from the GitHub runner?
You will need to listen to the 'error' event to catch this
const strPostData = querystring.stringify(postData);
options.headers['Content-Length'] = Buffer.byteLength(strPostData);
const req = http.request(options)
req.on('error', (err) => {/*Error Caught*/})
req.write(strPostData)
req.end()

Node js - Done is not a function after insertion in database

Im trying for the first time to do an authentification in node js. THe insertion in database is working but i've got an issue about the done function and i dont understand why. It's surely a simply answer but im still a beginner in node. Im using express and passeport for the authentification.
My code :
// DB
var LocalStrategy = require("passport- local").Strategy;
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);
// BD PASSEPORT
connection.query('USE ' + dbconfig.database);
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
connection.query("SELECT * FROM client WHERE id = ? ", [id],
function (err, rows) {
done(err, rows[0]);
});
});
passport.use(
'local-signup',
new LocalStrategy({
usernameField: 'username',
mailField: 'mail',
passwordField: 'password',
passReqToCallback: true
},
function (req, username, mail, password, done) {
connection.query("SELECT * FROM client WHERE username = ? ",
[username], function (err, rows) {
if (err)
return done(err);
if (rows.length) {
return done(null, false, req.flash('signupMessage', 'That is already taken'));
} else {
var newUserMysql = {
username: username,
mail: mail,
password: bcrypt.hashSync(password, null, null)
};
var insertQuery = "INSERT INTO client (username, mail, password) values (?, ?, ?)";
connection.query(insertQuery, [newUserMysql.username, newUserMysql.mail, newUserMysql.password],
function (err, rows) {
newUserMysql.id = rows.insertId;
console.log(newUserMysql);
return done(null, newUserMysql);
});
}
});
})
);
the problem is the last done funciton :
The output
{
username: 'eazeazea',
mail: 'eazeazeaze',
password: '$2a$10$iUOYeqzQqV6hJ.WvYMom8uesUxxtKpKfdb8GcoJ4Chkv ka.FIZfiO',
id: 20
}
D:\Projets\group-780053\node_modules\mysql \lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
TypeError: done is not a function
at Query.<anonymous> (D:\Projets\group-780053 \config\passport.js:51:44)
at Query.<anonymous> (D:\Projets\group-780053 \node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (D:\Projets\group-780053\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (D:\Projets\group-780053\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (D:\Projets\group-780053\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)
at Query.OkPacket (D:\Projets\group-780053\node_modules\mysql\lib\protocol\sequences\Query.js:74:10)
at Protocol._parsePacket (D:\Projets\group-780053\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Projets\group-780053\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Projets\group-780053\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Projets\group-780053\node_modules\mysql\lib\protocol\Protocol.js:38:16)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-loginregister# start: `node ./server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-loginregister# start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Nicol\AppData\Roaming\npm-cache\_logs\2020-07-16T10_30_48_382Z-debug.log
The passport.use when is use to rename the strategie the signature, is like this
passport.use('new-name', new LocalStrategy(options, (req, email, password, done) => {
// ....
}))
To be more specific to your example It should look like this.
passport.use('local-signup', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, function((req, email, password, next) {
// ...
})));
But in your case you passed 5 parameters, and the parameter which is named done in your case is the fifth parameter.
When passport will call that method function which is the verify as named in the code of passport-local sources
it will be passed only 4 parameters and they won't be any fifty parameter, so it will be undefined.
Your code just means you are trying to pass parameter to undefined which will obviously through an error. to fix that you will make this changes
replace this line
function (req, username, mail, password, done) { }
with
function (req, email, password, done) { }

Express - MySQL conn.end() is not functioning (Cannot enqueue after invoking quit)

I have already read the post Node Mysql Cannot Enqueue a query after calling quit, the conn.end() is in my query block.
My issue is that conn.end in my dashboard.js does not work and breaks the app with the following error.
I need to use it because MySQL connections are flooding the system till Database stop accepting more of them since they all stay open each time that are used.
For this post I will use only one (dashboard.js) of the several routes of my NodeJS application.
`Event.js` is not a part of my working files, probably is a file from `./node_modules`
|events.js:183
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Quit after invoking quit.
at Protocol._validateEnqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:204:16)
at Protocol._enqueue (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:139:13)
at Protocol.quit (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:92:23)
at Connection.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\Connection.js:249:18)
at ServerResponse.res.end (E:\NodeJS\Project-Change\node_modules\express-myconnection\lib\express-myconnection.js:114:54)
at ServerResponse.send (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:191:8)
at fn (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:896:10)
at View.exports.renderFile [as engine] (E:\NodeJS\Project-Change\node_modules\ejs\lib\ejs.js:323:3)
at View.render (E:\NodeJS\Project-Change\node_modules\express\lib\view.js:76:8)
at Function.app.render (E:\NodeJS\Project-Change\node_modules\express\lib\application.js:527:10)
at ServerResponse.res.render (E:\NodeJS\Project-Change\node_modules\express\lib\response.js:900:7)
at Query._callback (E:\NodeJS\Project-Change\routes\dashboard.js:39:17)
at Query.Sequence.end (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Query._handleFinalResultPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.EofPacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
at Protocol._parsePacket (E:\NodeJS\Project-Change\node_modules\mysql\lib\protocol\Protocol.js:279:23)
app.js (only relative lines)
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
app = express(),
expressValidator = require('express-validator'),
session = require('express-session'),
passport = require('passport'),
flash = require('connect-flash'),
passportConfig = require('./config/passport'),
dbConfig = require('./config/db');
// skipping code about static files, bodyparser, expressValidator, session, passport
passportConfig(passport)
/*MySQL connection*/
var connection = require('express-myconnection'),
mysql = require('mysql');
app.use(
connection(mysql, dbConfig,'request')
);
var dashboard = require('./routes/dashboard.js'); // in this route I apply conn.end()
var router = require('./routes/rates.js');
// skipping app.post/app.get code for /login & /logout & isLoggedIn middleware
app.use('/', router);
app.use('/', dashboard); // issue on that
app.get('/',function(req,res){
res.render('./dashboard.ejs'); //issue on that
});
module.exports = app;
routes/dashboard.js (route)
var express = require('express');
var router = express.Router();
var dashboard = express.Router();
dashboard.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});
var dashboard = router.route('/');
//show the CRUD interface | GET
dashboard.get(function(req,res,next){
req.getConnection(function(err,conn){
if (err) return next("Cannot Connect");
var query = conn.query('SELECT SUM(total_price) AS transactions_total FROM transactions WHERE date_created = CURDATE(); SELECT SUM(total_profit) AS total_profit FROM transactions', function(err,rows){
if(err){
console.log(err);
return next("MySQL error, check your query");
}
var ab = {data:rows[0]};
var total_profit = {data:rows[1]};
res.render('dashboard',{ab, total_profit});
conn.end(); // causes the described error
});
// conn.end(); even tried here
});
});
dashboard.all(function(req,res,next){
console.log("route for dashboard executed");
console.log(req.params);
next();
});
module.exports = router;
console.log('dashboard.js loaded!');
config/db.js
module.exports = {
host : 'localhost',
user : 'root',
password : '',
database : 'mydb',
multipleStatements: true,
debug : false
}
config/passport.js
External presentation in case is needed here

Trying to connect mLAB DB from localhost server? Authentication failed

Below is the code Snippet :
var mongoose = require('mongoose');
//mongodb://localhost/db
mongoose.connect('mongodb://username:pwd#ds117859.mlab.com:17859/db');
var db = mongoose.connection;
Now when I connect to localhost server, it works fine and I am able to perform operations on local Mongo DB
But when I connect to my db on MLAB, I get the following error:
$ node app.js
Server started on port 3000
(node:8648) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Authentication failed.
When I print the error object I get this :
err { MongoError: Authentication failed.
at C:\Users\user\Desktop\loginapp-master\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:595:61
at authenticateStragglers (C:\Users\user\Desktop\loginapp-master\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:513:16)
at Connection.messageHandler (C:\Users\user\Desktop\loginapp-master\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:549:5)
at emitMessageHandler (C:\Users\user\Desktop\loginapp-master\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:309:10)
at Socket.<anonymous> (C:\Users\user\Desktop\loginapp-master\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:452:17)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
errmsg: 'Authentication failed.',
code: 18,
codeName: 'AuthenticationFailed' }
//replace username,password and databasename
var mongoose = require('mongoose');
var mongodbUrl = "mongodb://username:password#ds153869.mlab.com:53869/databasename";
mongoose.connect(mongodbUrl, { useNewUrlParser: true });
mongoose.connection.on("connected", function(){
console.log("mongoose database connected with " + mongodbUrl);
});
mongoose.connection.on("error", function(err){
console.log("Unable to connect with " +mongodbUrl + "error are"+ err);
});

Node and MySQL connection to database

I need t o provide the MySQL connection for modules, so I provide the code like this:
var express = require('express');
var mysql = require('mysql')
var app = express();
var connection = mysql.createConnection({
host: '149.xxx.xx.x',
user: 'User',
password: 'Password',
database: 'DataBase',
port: 1443,
});
connection.connect(
function(error) {
console.log(error.code); // 'ECONNREFUSED'
console.log(error.fatal); // true
console.log(error.sql);
console.log(error.sqlMessage);
}
);
and after some time (about 1-2 minutes) I received an error from those console.logs:
ECONNRESET
true
undefined
undefined
I think maybe it's the timeout because of my "nonactivity", but when I'm trying to do the query the errors are the same.
Have any idea what is wrong? I checked it from DataGrip and the data are correct.
Ok, I found one small bug here. The database is MS SQL Server (microsoft). I'm trying now using the library called mssql to connect to that database, provide some code:
var config = {
server: "149.xxx.xx.x",
database: "Database",
user: "User",
password: "Password",
connectionTimeout: 300000,
requestTimeout: 300000,
pool: {
idleTimeoutMillis: 300000,
max: 100
}
};
function getEmp() {
var connection = new sql.Connection(config);
var req = new sql.Request(connection);
connection.connect(function (error) {
if(error) {
console.log(error);
return;
}
req.query('Procedure', function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
connection.close();
});
});
}
getEmp();
and I receive the error:
{ ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433
at Connection.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/mssql/lib/tedious.js:353:25)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:791:12)
at Socket.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:33:15)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
name: 'ConnectionError',
message: 'Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433',
code: 'ESOCKET' }
For sure the data are correct- DataGrip is connecting fine here.
I found at google the similar problem, the problem was Disabled TCP/IP. I checked this one and it's Enabled with this port 1443.