我目前正在使用 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而不是本地主机。
答案 0 :(得分:1)
尝试
sudo npm rebuild
在初始设置期间,某些软件包可能未正确安装。确保某些其他进程未使用端口27017
。
答案 1 :(得分:0)
bson模块可能未正确安装。
尝试 npm install -g node-gyp
然后
npm update