javascript - Node.js/MongoDB: How can I use module.exports to pass a localhost database url to server.js file? -
i have problem connection database mongoose on localhost.
in server.js file have:
var express = require('express'); var app = express(); //create our app express var mongoose = require('mongoose'); //mongoose mongodb var database = require('./config/database.js'); //load database config ... //configuration ===================== mongoose.connect('database.url'); //connect mongodb database ...
in database.js file have:
// config/database.js module.exports = { url : 'mongodb://127.0.0.1:27017/test' };
the error in node.js command prompt is:
events.js:141 throw er; // unhandled 'error' event ^ error: failed connect [database.url:27017] @ null.<anonymous> (c:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:25) @ emitthree (events.js:97:13) @ emit (events.js:175:7) @ null.<anonymous> (c:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:15) @ emittwo (events.js:87:13) @ emit (events.js:172:7) @ socket.<anonymous> (c:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:10) @ emitone (events.js:77:13) @ socket.emit (events.js:169:7) @ emiterrornt (net.js:1256:8)
when use mongodb://127.0.0.1:27017/test in place of database.url works fine.
i'm using node.js v4.2.6 , mongodb 3.2 on windows 10.
how can module.exports pass url server.js?
remove ''
, because trying connect mongodb url 'database.url'
, need use url
property database
contains right url mongodb://127.0.0.1:27017/test
mongoose.connect(database.url);
Comments
Post a Comment