Heroku Node deployment "internal server error" - javascript

I have my app deployed to heroku. Once I go to the user signup page and enter my credentials the site immediately crashes, displaying internal server error in the browser, and giving me this in the heroku logs.
I'm not sure what might be the issue so I don't know which code to give? I don't think it has to do with MySQL but considering it's mentioned in the error report, it might be? Either way here is my connection.js:
const mysql = require('mysql');
let connection;
if (process.env.JAWSDB_URL) {
connection = mysql.createConnection(process.env.JAWSDB_URL);
} else {
connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'trythis_db'
});
};
connection.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log('mysql is connected');
};
});
module.exports = connection;
This is my error code:
2017-12-04T04:58:51.486756+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2017-12-04T04:58:51.486150+00:00 app[web.1]: POST /signup 302 287.664 ms - 46
2017-12-04T04:58:51.486758+00:00 app[web.1]: at Object._errnoException (util.js:1024:11)
2017-12-04T04:58:51.486758+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1046:20)
2017-12-04T04:58:51.486759+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
2017-12-04T04:58:51.486760+00:00 app[web.1]: --------------------
2017-12-04T04:58:51.486761+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2017-12-04T04:58:51.486762+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:52:23)
2017-12-04T04:58:51.486763+00:00 app[web.1]: at PoolConnection.connect (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Connection.js:130:18)
2017-12-04T04:58:51.486763+00:00 app[web.1]: at Pool.getConnection (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:48:16)
2017-12-04T04:58:51.486764+00:00 app[web.1]: at Pool.query (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:202:8)
2017-12-04T04:58:51.486765+00:00 app[web.1]: at MySQLStore.set (/app/node_modules/express-mysql-session/lib/index.js:192:19)
2017-12-04T04:58:51.486766+00:00 app[web.1]: at Session.save (/app/node_modules/express-session/session/session.js:72:25)
2017-12-04T04:58:51.486766+00:00 app[web.1]: at Session.save (/app/node_modules/express-session/index.js:381:15)
2017-12-04T04:58:51.486767+00:00 app[web.1]: at ServerResponse.end (/app/node_modules/express-session/index.js:330:21)
2017-12-04T04:58:51.486768+00:00 app[web.1]: at ServerResponse.redirect (/app/node_modules/express/lib/response.js:947:10)
2017-12-04T04:58:51.638706+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2017-12-04T04:58:51.638708+00:00 app[web.1]: at Object._errnoException (util.js:1024:11)
2017-12-04T04:58:51.638709+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1046:20)
2017-12-04T04:58:51.638710+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
2017-12-04T04:58:51.638710+00:00 app[web.1]: --------------------
2017-12-04T04:58:51.638711+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2017-12-04T04:58:51.638712+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:52:23)
2017-12-04T04:58:51.638713+00:00 app[web.1]: at PoolConnection.connect (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Connection.js:130:18)
2017-12-04T04:58:51.638713+00:00 app[web.1]: at Pool.getConnection (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:48:16)
2017-12-04T04:58:51.638714+00:00 app[web.1]: at Pool.query (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:202:8)
2017-12-04T04:58:51.638802+00:00 app[web.1]: at session (/app/node_modules/express-session/index.js:460:11)
2017-12-04T04:58:51.638800+00:00 app[web.1]: at MySQLStore.get (/app/node_modules/express-mysql-session/lib/index.js:130:19)
2017-12-04T04:58:51.638803+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-12-04T04:58:51.638804+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2017-12-04T04:58:51.638804+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7

The first line of the error (Error: connect ECONNREFUSED 127.0.0.1:3306) says that the app is trying to connect to localhost even when it is hosted online, where it should try to connect to the online database. You need to figure out what's wrong in the if-else statements, since, the else loop is being executed, which, I guess, is for your local environment.

Make sure you have added JAWSDB_URL under project config variables.
(Heroku project -> settings -> config variables -> reveal variables -> your var)

Issue was with another library related to storing sessions in MySQL...

Related

Can't connect to remote db (Hostinger)

I've been having this error for a long time and I can't solve it. I have made my project locally, connecting to my database locally. Once finished I wanted to migrate my database manually since it is small to my hosting provider.
I have created the database, I have given it permissions to connect remotely and I have also checked that the connection data is correct.
A curiosity is that through mysql workbench it has allowed me to connect, the problem is clearly with my code, I am working with NodeJS (express). Another curiosity is that it appears to me as if I have successfully connected and 30 seconds later it gives me the following error:
Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Listen on port 3000
Connection success!
node:events:491
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (node:events:513:28)
at Protocol._delegateError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...
This is my connection script:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
connection.connect((error) => {
if(error){
console.log('The connection error is: ' + error)
return;
}
console.log('Connection success!')
})
module.exports = connection;
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
// Now use the connection to do stuff
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Result: " + result);
});
});
You don't show the rest of your code, but you're not doing any query in that callback function, just outputting success. You need to do stuff inside that callback, or change the way you're exporting the connection. Have a look at createPool, and getConnection as I'm guessing you're actually wanting a persistent connection to your database?

Server crashes when making an API call

app.post("/", (req, res) => {
const origin = req.body.origin;
const destination = req.body.destination;
const urlOri = "https://api.openweathermap.org/data/2.5/weather?q=" + origin + "&APPID={mykey}"
https.get(urlOri, function (response) {
console.log(response);
})
});
I have written this code and I am getting this error:
Server started on port 443
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 178.128.25.248:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '178.128.25.248',
port: 443
}
This same code runs on one of my friend's pc same as expected, what to do?
I have tried to do the same via request and axios module as well and I got the same error.
Sorry guys I was using a proxy server.
I disabled it and now it's working fine

node.js express permission error on linux

using arch with admin user account and no sudo on this script:
var express = require('express');
var fs = require('fs');
var app = express();
app.get('/lol', function(req, res) {
res.sendFile('second.html', {root: __dirname })
});
var port = process.env.PORT || 81;
var server = app.listen(port);
i get this error that didnt change when i changed the port or the url to trigger it
it instantly gives me this error after i run the command: node site.js
(the code above is site.js)
node:events:371
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 0.0.0.0:81
at Server.setupListenHandle [as _listen2] (node:net:1302:21)
at listenInCluster (node:net:1367:12)
at Server.listen (node:net:1454:7)
at Function.listen (/home/{name}/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/{name}/Documents/web/demo/site.js:10:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1346:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -13,
syscall: 'listen',
address: '0.0.0.0',
port: 81
}
if i run the script with sudo it works fine but i dont want to run it in sudo
beacuse i have to run it on a server with no sudo. any help?
Using port below 1024 without root permission is common issue. Try port bigger than 1024.

Failed to torify node application

I'm writing a simple node server, and trying to torify it. But I got the following error, any idea?
It works fine when I go without torsocks, and I used default config for tor daemon.
$ torsocks node server.js
[Jan 26 09:38:40] WARNING torsocks[30933]: [syscall] Unsupported syscall number 293. Denying the call (in tsocks_syscall() at syscall.c:465)
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EPERM :::5000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at Object.<anonymous> (/home/test/src/heroku/ruten-helper/server.js:10:8)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
var http = require('http')
// Create server
var server = http.createServer(function (req, res) {
console.log("client connected")
res.end('HI!')
})
// Listen
server.listen(process.env.PORT || 5000)
I got it.
I checked the torsocks debug message, I see the following.
[listen] Non localhost inbound connection are not allowed. (in tsocks_listen() at listen.c:64)
And I changed my code to listen on localhost only, problem resolved.

Site.js connection timeout

I'm trying to launch site.js script on my page(http://www.erzis.ct8.pl), but it keeps showing me this error.
[2017-01-04 16:42:34.936] [TRACE] [default] - Strange error
[2017-01-04 16:42:34.936] [DEBUG] [default] - { [Error: listen EPERM 0.0.0.0:8080]
code: 'EPERM',
errno: 'EPERM',
syscall: 'listen',
address: '0.0.0.0',
port: 8080 }
Error: listen EPERM 0.0.0.0:8080
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at Server._listen2 (net.js:1237:19)
at listen (net.js:1286:10)
at Server.listen (net.js:1382:5)
at Server.listen.Server.attach (/usr/home/erzis/domains/erzis.ct8.pl/public_html/Bot/BOT/node_modules/socket.io/lib/index.js:228:9)
at null._onTimeout (/usr/home/erzis/domains/erzis.ct8.pl/public_html/Bot/BOT/site.js:618:29)
at Timer.listOnTimeout (timers.js:92:15)

Categories

Resources