Node with mongodb not starting application

Asked

Viewed 152 times

0

I’m trying to raise an application in Node with mongoose, (I am studying, MEAN book code house) and generates me the following error while trying to start the file server.js where I start my application on Node, see below.

comando para iniciar o server NODE

Below is my server.js file.

var http = require('http');

var app = require('./config/express')();

require('./config/database.js')('mongodb://localhost/contatooh');

http.createServer(app).listen(app.get('port'),function(){
	console.log('Express server executando na porta ' + app.get('port'));
});

NOTE: the file path bson.node is correct, and the file is existing in the folder.

Below is the connection file with the bank Mongodb.

var mongoose = require('mongoose');

module.exports = function(uri){
    mongoose.connect(uri);

    mongoose.connection.on('connected', function(){
        console.log('Mongoose ! conectado em '+uri);
    });

    mongoose.connection.on('disconnected', function(){
        console.log('Mongoose ! Desconectado de '+uri);
    });

    mongoose.connection.on('error', function(erro){
        console.log('Mongoose ! erro na conecxão  '+erro);
    });

    process.on('SIGINT', function(){
        mongoose.connection.close(function(){
            console.log('Mongoose ! Desconectado pelo término da aplicação');
            process.exit(0);
        });
    });
}

  • Hello, blz? do you have this code on github? ...I find it strange that line 5: require('. /config/database.js')('mongodb://localhost/contactooh');

  • Oops ! blz and vc ? Thanks for the feedback, I shared it on github so you can have a look. Link: https://github.com/LucasPiresDev/mean_stack vlw !!!

2 answers

0


Man, I got some suspicions:

1º - try to run this project from another folder: it can be something related to windows permissions or some firewall that is blocking the process...

2º - The version of Node - I went around in a linux environment in the cloud (AWS-Cloud9) your project, I used Node V6.11.2 - but had to change line 4 in the database.js:

    mongoose.connect(uri, { useNewUrlParser: true });

I hope it gives you a light.

  • Oops, blz Valter ! Face, it makes sense the two suspicions, I’ll have to test in another environment to see if it’s not the S.O. that stopped that service. If this does not work I will try to change the version of Node, I am in version 8.11.2, so I would have to install this version that commented. I will test and comment here the result of both, Vlw!

  • I did a test here and nothing, I came back version of Node and put in another machine, and still can not make run. Well I did the following test, I took out the modulo Mongoose and left Mongo pure, and tried to run a simple query through the terminal and nothing happens when running. I don’t know what to do .

  • Problem SOLVED, I was using the outdated mongodb driver, making the update everything worked perfectly. Thanks to everyone who helped me to find a solution, I hope my experience will help someone.Hug!

0

To avoid future deprecation errors, the most recent correction is this:

const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/your_database', 
    { useNewUrlParser: true, useUnifiedTopology: true }).
    catch(error => handleError(error));

mongoose.set('useCreateIndex', true)
mongoose.set('useFindAndModify', false)

mongoose.connection.on('error', err => {
    logError(err);
});

mongoose.Promise = global.Promise

module.exports = mongoose

Works on version 5.7.1 of Mongoose. Source: Mongoose

Browser other questions tagged

You are not signed in. Login or sign up in order to post.