我有一個連接到本地主機上貓鼬數據庫的問題。Node.js/MongoDB:如何使用module.exports將localhost數據庫url傳遞給server.js文件?
在我server.js文件我有:
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
...
在我database.js文件我有:
// Config/database.js
module.exports = {
url : 'mongodb://127.0.0.1:27017/test'
};
我在node.js中得到的錯誤命令提示符:
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)
當我使用mongodb://127.0.0.1:27017/test代替database.url時,它工作得很好。
我目前使用的Node.js v4.2.6和Windows 10
我怎樣才能得到module.exports MongoDB的3.2傳遞的URL server.js?