我目前正在尝试使用MongoDB,Express,Swig和NodeJS的简单Hello World应用程序
使用最新节点以及其他依赖项。
Chrome 46.0(64位)。 Mac OS X 10.9.5
以下是我的app.js代码
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server;
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
var mongoclient = new MongoClient(new Server("localhost", 27017, {'native_parser':false}));
var db = mongoclient.db('course');
app.get('/', function(req, res){
// Find one document in our collection
db.collection('hello_mongo_express').findOne({}, function(err, doc) {
if(err) throw err;
res.render('hello', doc);
});
});
app.get('*', function(req, res){
res.send('Page Not Found', 404);
});
mongoclient.open(function(err, mongoclient) {
if(err) throw err;
app.listen(8080);
console.log('Express server started on port 8080');
});
以下是节点app.js时出现的错误:
AMAC02PC0PHG3QP:6_hello_world_express_swig_mongodb macadmin$ node app.js
Failed to load c++ bson extension, using pure JS version
/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/app.js:31
if(err) throw err;
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/node_modules/mongodb/lib/mongodb/connection/server.js:553:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/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> (/Users/macadmin/Desktop/NodeJS_MongoDB/6_hello_world_express_swig_mongodb/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1250:8)
有些地方说写127.0.0.1代替本地主机。
最佳答案
尝试sudo npm rebuild
在初始设置过程中,某些软件包可能未正确安装。确保其他进程未使用端口27017
。
关于javascript - NodeJS的MongoDB连接错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33154640/