2

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
Sriram R
  • 2,109
  • 3
  • 23
  • 40
  • did you start the server in the terminal? read this: https://stackoverflow.com/questions/6478113/unable-to-start-mongodb-local-server – zb22 Feb 16 '18 at 11:55
  • I only did nodemon app in the terminal. I didn't start the mongo db server seperately. Are we supposed to do that? – Sriram R Feb 16 '18 at 11:59
  • use ```mongod``` in terminal and then start app in another terminal. – Lalit Goswami Feb 16 '18 at 11:59

2 Answers2

1

Open terminal/CMD and start your MongoDB server using command mongod

This command will start MongoDB for you and so your app can connect to it on localhost:27017

Kush Vyas
  • 5,813
  • 2
  • 26
  • 36
sidgujrathi
  • 479
  • 4
  • 7
  • I tried. It was shutting down immediately. It gives a NonExisting path for /data/db but I have that folder created – Sriram R Feb 16 '18 at 12:01
  • If you are using Windows create one dir on C: drive called `data` and if you are using linux then create `data` folder in `/hoome` – sidgujrathi Feb 16 '18 at 12:02
  • Im using mac and I have the data folder inside the mongodb folder in the system – Sriram R Feb 16 '18 at 12:07
  • Then try `sudo mkdir -p /data/db` and try starting server again using `mongod` – sidgujrathi Feb 16 '18 at 12:09
  • I think it is problem of directory permission. Try changing permission of data directory using `sudo chown -R 'youruserid' /data/db` or `sudo chmod 0755 /data/db` or `sudo chown $USER /data/db` – sidgujrathi Feb 16 '18 at 12:19
  • Yeah that was the problem. I just fixed it and was gonna comment here – Sriram R Feb 16 '18 at 12:27
  • 2
    Great!! Glad it worked for you. Do accept this answer and up vote to support and build better developer community. Thank you – sidgujrathi Feb 16 '18 at 12:28
0

Are you sure you are running mongo? Your URL points to localhost, which means you need to be serving it (Separate from serving your app with nodemon).

This link shows how to run mongo from shell