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
Related
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?
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 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...
i am very new to nodejs
when i tried to run my first js file having following content
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
with the command
$ node fullpath/filename.js
i am getting error thrown like below
Can anyone tell what is wrong. I am using nodejs appliction window
Please do not run the code in the REPL
[user#machine]$node
> node app.js
SyntaxError: Unexpected identifier
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:225:25)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)
Just run it in the command line
[user#machine]$node app.js
Server running at http://127.0.0.1:8081/
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