Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Killeon Patterson
Killeon Patterson
18,527 Points

MongoDB Connection Error

I'm having issues connecting my Express App to Mongoose. After I type npm start nodemon mtmarket in the terminal I get error messages -connection error { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED] at Pool.<anonymous> (C:\mtmarket\node_modules\mongodb-core\lib\topologies\server.js:505:11)

The path to my project is /mtmarket.

The path to the Mongdb bin is /Program Files/MongoDB/Server/3.4/bin

I've installed mongoose in my project and run mongod in a separate terminal from the bin. The terminal says MongoDB starts and then it automatically ends.

2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] MongoDB starting : pid=1348 port=27017 dbpath=C:\data\db\ 64-bit host=Owner-VAIO 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] db version v3.4.15.50-g26706cb04d 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] git version: 26706cb04de995a2d7252da936ba815217239a60 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2o-fips 27 Mar 2018 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] allocator: tcmalloc 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] modules: non 2018-06-18T08:21:31.485-0700 I CONTROL [initlisten] build environment: 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] distmod: 2008plus-ssl 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] distarch: x86_64 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] target_arch: x86_64 2018-06-18T08:21:31.485-0700 I CONTROL [initandlisten] options: {} 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating

2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] shutdown: going to close listening sockets... 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] shutdown: going to flush dialog... 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] now exiting 2018-06-18T08:21:31.484-0700 I CONTROL [initandlisten] shutting down with code:100

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var index = require('./routes/index');

var app = express();

//mongoose connect'
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/register', index);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);

module.exports = app;

Thank you for your help.

2 Answers

Ken Alger
Ken Alger
Treehouse Teacher


If you read through that trace, mongod can't find a c:\data\db directory. You'll need to create that and make sure that folder has write permissions.

Post back with further questions.


Ken Alger
Ken Alger
Treehouse Teacher


Can you post the full message you get when you try to run mongod? If that process isn't starting your MongoDB Server won't be running so you won't have anything to connect to.


Killeon Patterson
Killeon Patterson
18,527 Points

Hello Ken,

I've updated the process.

Thanks, Killeon