-1
新的Express App,我試圖用express和node.js來設置mongodb。我有一些問題,如果我能在這方面得到一些幫助。我試圖添加目錄refrences,所以你可以有想法。如何用express設置mongodb?
1- var config
即將來臨undefined不確定路徑是否正確。
2-正在語法錯誤:意外的保留字爲 require('./config/express')(app);
。
基本上試圖讓本地mongodb運行應用程序。
服務器> app.js
var express = require('express');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var config = require('./config/environment');
var http = require('http');
var https = require('https');
var fs = require('fs');
var bodyParser = require('body-parser');
// Connect to MongoDB
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
});
// Setup server
var app = express();
var server = http.createServer(app);
require('./config/express')(app);
require('./routes')(app);
// Start server
function startServer() {
server.listen(config.port, config.ip, function() {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
}
setImmediate(startServer);
// Expose app
exports = module.exports = app;
服務器>配置> express.js
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var path = require('path');
var config = require('./config/environment');
var session = require('express-session');
var connectMongo = require('connect-mongo');
var mongoose = require('mongoose');
var mongoStore = connectMongo(session);
export default function(app) {
var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
/*app.use(compression());*/
app.use(bodyParser.urlencoded({
extended: false
}));
app.set('appPath', path.join(config.root, 'client'));
if ('development' === env || 'test' === env) {
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
服務器>配置>環境> development.js正如有人指出
module.exports = {
// MongoDB connection options
mongo: {
uri: 'mongodb://localhost/test-modeler-dev'
},
// Seed database on startup
seedDB: false
};
如果不知道'。/ config'目錄結構是什麼,很難確定問題。從它的外觀來看,'。/ config/environment'目錄中沒有'index.js'文件,因此它是未定義的。必須嘗試使用像require('./ config/environment/development.js')'這樣的特定文件嗎? –
配置位於服務器下,服務器位於根級別,正如我在development.js中提到的那樣 – hussain
「./config/ environment」目錄中還有其他文件嗎?如果只有'development.js',那麼你需要使用'require('./ config/environment/development.js')',因爲默認情況下模塊加載器不會解析該名稱。 –