当我尝试运行“http://localhost:3000/light”时,在 CURL 中收到错误“来自服务器的空回复”
var express = require('express'),
values = require('./routes/values');
var app = express();
app.get('/light', values.findAll);
app.listen(3000);
console.log('Listening on port 3000...');
我的 values.js 是
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sensordb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sensordb' database");
db.collection('values', {safe:true}, function(err, collection) {
if (err) {
console.log("The 'values' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findAll = function(req, res) {
db.collection('values', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
var populateDB = function() {
var values = [
{
value: "10",
date: "121212",
time: "1214"
},
{
value: "12",
date: "121212",
time: "1224"
}];
db.collection('values', function(err, collection) {
collection.insert(values, {safe:true}, function(err, result) {});
});
};
基本上在上面的代码中,我创建了一个数据库,如果数据库为空,我尝试填充它。当我在终端上运行服务器代码时,我也会得到类似的东西:
= Please ensure that you set the default write concern for the database by setting =
= one of the options =
= =
= w: (value of > -1 or the string 'majority'), where < 1 means =
= no write acknowlegement =
= journal: true/false, wait for flush to journal before acknowlegement =
= fsync: true/false, wait for flush to file system before acknowlegement =
= =
= For backward compatibility safe is still supported and =
= allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =
= the default value is false which means the driver receives does not =
= return the information of the success/error of the insert/update/remove =
= =
= ex: new Db(new Server('localhost', 27017), {safe:false}) =
= =
= http://www.mongodb.org/display/DOCS/getLastError+Command =
= =
= The default of no acknowlegement will change in the very near future =
= =
= This message will disappear when the default safe is set on the driver Db =
========================================================================================
上述错误是否引起了一些问题?如果是这样,是什么?如果不是,那有什么责任?
任何帮助将非常感激。