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?
Related
I am making trying to take a backend using Hapi for the first time but every time a request is sent to the server it crashes. Sometimes I do get a response but the server eventually crashes on its own.
The error I get is:
TypeError: Cannot read properties of null (reading 'statusCode')
at Request._finalize (C:\Users\prakh\Desktop\Angular\buy-and-sell-backend\node_modules\#hapi\hapi\lib\request.js:491:31)
at Request._reply (C:\Users\prakh\Desktop\Angular\buy-and-sell-backend\node_modules\#hapi\hapi\lib\request.js:428:18)
at Request._execute (C:\Users\prakh\Desktop\Angular\buy-and-sell-backend\node_modules\#hapi\hapi\lib\request.js:274:14)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
The code is simple since I am only testing right now:
import Hapi from '#hapi/hapi'
const start = async () => {
const server = Hapi.server({
port: 8000,
host: 'localhost',
});
server.route({
method: 'GET',
path: '/hello',
handler: (req, h) => {
return 'Hello!';
}
});
await server.start();
console.log(`Server is listening on ${server.info.uri}`)
}
process.on('unhandledRejection', err => {
console.log(err);
process.exit(1);
});
start();
I am using Node v16.17.0 and the command I use to run it is npx babel-node src/server.js
I am not sure what I am doing wrong here.
Found the fix.
Had to update the Hapi version that I was using.
Use: npm uninstall #hapi/hapi
and then: npm install #hapi/hapi
Hapi 20.2.2 works
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
I am trying to connect node.js to MySQL and failed. I have installed MySQL and relevant libraries. How do I resolve this error? Also, if I want to get data to react-native, how should I go about doing it?
const express = require('express');
const mysql = require('mysql');
const connection = mysql.createPool({
host : '*****',//aws db endpoint/MySQL hostname
user : 'administrator', // username of MySQL connection
password : '*****', //pw of MySQL db
database : 'database_3' // name of database to be used
});
const app = express();
app.get('/User', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query('SELECT * FROM User', function (error, results, fields) {
if (error) throw error;
res.send(results)
});
});
});
// Starting our server.
app.listen(3306, () => {
console.log('Go to http://localhost:3306/User');
});
The error msg received :
events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1279:14)
at listenInCluster (net.js:1327:12)
at Server.listen (net.js:1414:7)
at Function.listen (C:\Users\Irvin\my-new-project\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Irvin\my-new-project\route.js:39:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1306:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Error: listen EADDRINUSE: address already in use :::3306
This means that the port 3306 is already in use, try changing it to another port or stop the process running on that port.
// Lets change 3306 to 3307
app.listen(3307, () => {
console.log('Go to http://localhost:3307/User');
});
If you want to stop the process on this port here is how to do it on linux
3306 is the default mysql port. Don't use that port for a server if you already have mysql running.
Don't stop the process on that port otherwise mysql will stop running, unless you're running it in another port, which is not your case.
app.listen(3000, () => {
console.log('Go to http://localhost:3306/User');
});
Hi since you are running mysql, it runs on port 3306 by default. Since you are tryung to start your express app on the same port, you get this error. You need to start your express application on any other port.
Your nodeJs application is using same port with MySQL .
// Starting our server.
app.listen(3306, () => {
console.log('Go to http://localhost:3306/User');
});
That app.listen(3306) is your nodeJs application listening port,
your current MySQL listening port is also 3306 ,
so your nodeJs application throw an error
Error: listen EADDRINUSE: address already in use :::3306
Means the port 3306 is already in use .
You should change your nodeJs application listening port like 8080 or 8090,
For example :
const express = require('express');
const mysql = require('mysql');
const connection = mysql.createPool({
host : '*****',//aws db endpoint/MySQL hostname
user : 'administrator', // username of MySQL connection
password : '*****', //pw of MySQL db
database : 'database_3' // name of database to be used
});
const app = express();
app.get('/User', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query('SELECT * FROM User', function (error, results, fields) {
if (error) throw error;
res.send(results)
});
});
});
// change this port to 8090
app.listen(8090, () => {
console.log('Go to http://localhost:3306/User');
});
This question already has answers here:
ECONNREFUSED error when connecting to mongodb from node.js
(21 answers)
Closed 5 years ago.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err)
{
throw err;
}
console.log("Database created!");
db.close();
});
This is the error I am getting when executing this code in nodejs and trying to create a database, I was going through the tutorial in w3school, please help to solve this error--
C:\Users\n.patel\Desktop\Promotion\nodejs\application4success>node mymongodb.js
C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\lib\mongo_client.js:415
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:3
29:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:280
:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:187:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
Make sure that you have Mongo running on on port 27017
use this to verify
mongo localhost:27017
I am currently trying a simple Hello World App using MongoDB, Express, Swig and NodeJS
Using latest node, and other dependencies.
Chrome 46.0 (64 bit). Mac OS X 10.9.5
Below is my app.js code
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server;
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
var mongoclient = new MongoClient(new Server("localhost", 27017, {'native_parser':false}));
var db = mongoclient.db('course');
app.get('/', function(req, res){
// Find one document in our collection
db.collection('hello_mongo_express').findOne({}, function(err, doc) {
if(err) throw err;
res.render('hello', doc);
});
});
app.get('*', function(req, res){
res.send('Page Not Found', 404);
});
mongoclient.open(function(err, mongoclient) {
if(err) throw err;
app.listen(8080);
console.log('Express server started on port 8080');
});
Below is the Error I get when node app.js:
AMAC02PC0PHG3QP:6_hello_world_express_swig_mongodb macadmin$ node app.js
Failed to load c++ bson extension, using pure JS version
/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/app.js:31
if(err) throw err;
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/node_modules/mongodb/lib/mongodb/connection/server.js:553:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1250:8)
Some places it is saying to write 127.0.0.1 instead of local host.
Try
sudo npm rebuild
Some of the packages may not have been installed properly during the initial set up. Make sure port 27017 is not in use by some other process.
The bson module is maybe not correctly installed.
Try
npm install -g node-gyp
and then
npm update