I am new in UI field. I have an issue connecting the database locally. Is there any error in my code? I went through many threads but not resolved yet. Please help! This is server.js :
var express = require('express');
var app = express();
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient
var dbOperation = null;
MongoClient.connect('mongodb://localhost:27017/training', function(err, db) {
if (err) throw err
dbOperation = db;
})
app.use('/mahadev', express.static('src'));
app.get('/todo', function(req, res) {
dbOperation.collection('todo').find().toArray(function (err, result){
if (err) throw err
res.send(result);
console.log(result)
})
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
})
command prompt says:
C:\Users\dhava>node server.js
Example app listening on port 3000!
C:\Users\dhava\node_modules\mongodb\lib\mongo_client.js:336
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect
at Pool.<anonymous> (C:\Users\dhava\node_modules\mongodb-core\lib\topologies\server.js:326:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\dhava\node_modules\mongodb-core\lib\connection\pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\dhava\node_modules\mongodb-core\lib\connection\connection.js:175:49)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
Browser says:
This site can’t be reached
localhost refused to connect.
Search Google for localhost 3000 todo
ERR_CONNECTION_REFUSED
Thank you in advanced :)
via Dhaval J. Patel
No comments:
Post a Comment