I am starting to learn NodeJS and Express framework. I was following a tutorial where I had a basic application
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const app = express();
mongoose.Promise = global.Promise
// mongoose
mongoose.connect('mongodb://localhost/vidjot-dev')
.then(() => console.log("Mongo DB started"))
.catch(err => console.error(err));
// handlebars middleware
app.engine('handlebars', exphbs({
defaultLayout: 'main'
}));
app.set('view engine','handlebars')
// index route
app.get('/', (req, res) =>{
const title = "Welcome"
res.render('index',{
title : title
});
});
app.get('/about', (req,res) =>{
res.render('about');
});
const port = 5000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
I tried to start the node js server from the console using the command
nodemon app
Where app is the name of the JS file app.js
When I tried to start the server, I got this error
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/topologies/server.js:503:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/connection/pool.js:326:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }
I tried setting the DB path again but it wasn't of any use. I tried to start mongod from the terminal but got the same error again. How do I fix this?
EDIT: I have the directories data/db created and I just ran
mongod --dbpath <DIRECTORY>
and got this error
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] MongoDB starting : pid=76227 port=27017 dbpath=/Users/sriramr/mongodb/data/db 64-bit host=MACBOOKs-MacBook-Air.local
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] db version v3.6.2
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] allocator: system
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] modules: none
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] build environment:
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] distarch: x86_64
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] target_arch: x86_64
2018-02-16T17:42:02.657+0530 I CONTROL [initandlisten] options: { storage: { dbPath: "/Users/sriramr/mongodb/data/db" } }
2018-02-16T17:42:02.659+0530 I - [initandlisten] Detected data files in /Users/sriramr/mongodb/data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-02-16T17:42:02.660+0530 I STORAGE [initandlisten] exception in initAndListen: InvalidOptions: Requested option conflicts with current storage engine option for directoryPerDB; you requested false but the current server storage is already set to true and cannot be changed, terminating
2018-02-16T17:42:02.661+0530 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2018-02-16T17:42:02.661+0530 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2018-02-16T17:42:02.661+0530 I CONTROL [initandlisten] now exiting
2018-02-16T17:42:02.661+0530 I CONTROL [initandlisten] shutting down with code:100