【发布时间】:2015-08-30 12:37:59
【问题描述】:
我在尝试使用 Node.ja 和 Mongodb 运行服务器时遇到以下错误。
错误:
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
C:\xampp\htdocs\odiya_chat\server.js:15
db = mongo.connect("127.0.0.1:27017/"+db, collections);
^
TypeError: Object function (connString, cols) {
var dbname = getDbName(connString);
var onserver = thunky(function(cb) {
getTopology(connString, function(err, topology) {
if (err) return cb(err);
cb(null, topology);
});
});
if (!dbname) {
dbname = connString._dbname;
onserver = thunky(function(cb) {
toMongodbCore(connString, function(err, server) {
if (err) cb(new Error('You must pass a connection string or a mongojs in
stance.'));
cb(null, server);
});
});
}
var that = new Database({name: dbname, cols: cols}, onserver);
if (typeof Proxy !== 'undefined') {
var p = Proxy.create({
get: function(obj, prop) {
if (that[prop]) return that[prop];
that[prop] = that.collection(prop);
return that[prop];
}
});
return p;
};
return that;
} has no method 'connect'
at Object.<anonymous> (C:\xampp\htdocs\odiya_chat\server.js:15:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
错误 2
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
我的服务器端代码如下。
服务器.js
var port=8888;
var express=require('express');
var http=require('http');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);
var mongo = require('mongojs');
var app=express();
var server=http.createServer(app);
db = 'doctor',
collections = ['oditek'],
db = mongo.connect("127.0.0.1:27017/"+db, collections);
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride());
app.use(cookieParser());
//app.use(expressSession({secret:'somesecrettokenhere'})); // simulate DELETE and PUT
app.get('/',function(req,res){
res.sendfile('view/index.html');
})
app.get('/login',function(req,res){
res.sendfile('view/login.html');
});
app.get('/chatroom',function(req,res){
if(req.session){
res.sendfile('view/chatroom.html');
}
});
server.listen(port);
console.log('server is running on the port'+port);
我的要求是,如果用户已经登录客户端,他将获得 chatroom.html 页面,如果不在客户端,它将要求您登录。我在这里实现 connect-mongodb-session 以存储会话。我有阅读有关此的一些帖子并尝试了答案,但仍然存在错误。请帮助我解决此错误。
【问题讨论】:
标签: javascript node.js mongodb