在写这个问题之前,我正在阅读其他关于几乎相同错误的问题,但问题是从2013年到2014年,并且解决方案不起作用,我使用的是ElementaryOS(Ubuntu 14.04)。 / p>
我想管理我的nodejs应用程序上的会话,这些会话将被上传到OpenShift服务器,所以在搜索了会话存储备选方案后,我找到了connect-mongo
,在我阅读时,将会话存储在mongodb集合中。我也在使用express-sessions
,事实是,当我运行应用程序时,我收到了此错误消息:
hugo@HugoG405:~/Documents/ic/server$ node server
module.js:341
throw err;
^
Error: Cannot find module 'mongodb/node_modules/bson'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/hugo/Documents/ic/server/node_modules/express-sessions/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js:8:16)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/hugo/Documents/ic/server/node_modules/express-sessions/node_modules/mongoose/lib/types/objectid.js:18:16)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
我是会话和存储的新手,所以我真的不知道发生了什么以及如何正确设置会话存储。我真的需要一些帮助,如果有人能告诉我如何解决这个问题,我会非常感激。
以下是我的package.json
依赖项:
"engines": {
"node": ">= 0.6.0",
"npm": ">= 1.0.0"
},
"dependencies": {
"body-parser": "^1.15.0",
"connect-mongo": "^1.1.0",
"express": "^4.13.4",
"express-sessions": "^1.0.5",
"helmet": "^1.3.0",
"method-override": "^2.3.5",
"mongoose": "^4.4.12",
"pug": "^2.0.0-alpha6"
},
"private": true,
"main": "server.js",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-autoprefixer": "^3.1.0",
"gulp-concat": "^2.6.0",
"gulp-image": "^1.3.1",
"gulp-jshint": "^2.0.0",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.3.1",
"gulp-sourcemaps": "^2.0.0-alpha",
"gulp-uglify": "^1.5.3"
}
这是我的configurate.js
文件,用于设置中间件,路由和数据库连接:
var express = require('express'),
bodyParser = require('body-parser'),
helmet = require('helmet'),
methodOverride = require('method-override'),
sessions = require('express-sessions'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo/es5')(sessions);
// Rutas de la plataforma
var toHome = require('./routes/Home'),
toVideos = require('./routes/Video'),
toUsers = require('./routes/User'),
toCourses = require('./routes/Course'),
toRedirects = require('./routes/Redirects');
var connection_string = '127.0.0.1:27017/e3';
if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}
mongoose.connect("mongodb://"+connection_string);
var db = mongoose.connection;
db.on('error', function(){
console.error.bind(console, 'Conection error to db:');
});
db.on('open', function() {
console.log('Conected to mongodb');
});
module.exports = function(app){
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use(express.static('public'));
app.use(helmet());
app.set('view engine','pug');
app.use(sessions({
secret:"23Ir892UdRHoi1rN891",
resave:false,
saveUninitialized:false,
store: new MongoStore({
url:connection_string,
ttl: 24 * 60 * 60
})
}));
// Routing
app.use('/',toRedirects);
app.use('/platform',toHome);
//app.use('/platform/video',toVideos);
//app.use('/platform/profile', toUsers);
//app.use('/platform/course', toCourses);
}
如果您需要,我的server.js
文件:
var express = require('express'),
app = express(),
models = require('./config/models'),
Configure = require('./config/configure');
Configure(app);
// Página principal
app.get('/', function(req,res){
res.render('index');
});
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.listen(server_port,server_ip_address);
答案 0 :(得分:1)
我认为您没有安装此模块。
npm install connect-mongodb-session --save
这将解决问题。
答案 1 :(得分:0)
您好请按照以下步骤重新安装节点模块。
让我们知道它是否适合您。