I have a problem with connection to a database with mongoose on localhost.
In my server.js file I have:
var express = require('express'); var app = express(); //Create our app with express var mongoose = require('mongoose'); //Mongoose for mongoDB var database = require('./config/database.js'); //Load the database config ... //Configuration ===================== mongoose.connect('database.url'); //Connect to mongoDB database ... In my database.js file I have:
// Config/database.js module.exports = { url : 'mongodb://127.0.0.1:27017/test' }; The error I get in my node.js command prompt is:
events.js:141 throw er; // Unhandled 'error' event ^ Error: failed to connect to [database.url:27017] at null.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:25) at emitThree (events.js:97:13) at emit (events.js:175:7) at null.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:15) at emitTwo (events.js:87:13) at emit (events.js:172:7) at Socket.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:10) at emitOne (events.js:77:13) at Socket.emit (events.js:169:7) at emitErrorNT (net.js:1256:8) When I use mongodb://127.0.0.1:27017/test in place of database.url it works just fine.
I'm currently using node.js v4.2.6 and mongodb 3.2 on Windows 10.
How can I get module.exports to pass the url to the server.js?
2 Answers
Remove '', because now you are trying connect to mongodb with url 'database.url', but you need use url property from database that contains right url mongodb://127.0.0.1:27017/test
mongoose.connect(database.url); 0Have you tried taking it out of speech marks?
mongoose.connect('database.url'); is trying to connect to the address 'database.url'
mongoose.connect(database.url); will get the url property of database