我在Cubian上安装了Cubieboard操作系统。 然后我使用这个tutorial安装了node.js和mongoDB。 似乎node.js正常工作。我可以开始mongod服务,但我有几个问题:
npm install
产生了很多错误,我无法使用此命令安装所需的模块(但我仍然可以使用nmp install module_name
命令)我的测试node.js代码:
var express = require('express');
// Mongoose import
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', console.error);
db.once('open', function() {
// Create your schemas and models here.
console.log('Connected');
});
// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://127.0.0.1:27017/test', function (error) {
if (error) {
console.log(error);
}
});
// Mongoose Schema definition
var Schema = mongoose.Schema;
var UserSchema = new Schema({
first_name: String,
last_name: String,
email: String
});
// Mongoose Model definition
var User = mongoose.model('users', UserSchema);
// Bootstrap express
var app = express();
// URLS management
app.get('/', function (req, res) {
res.send("<a href='/users'>Show Users</a>");
});
app.get('/users', function (req, res) {
User.find({}, function (err, docs) {
res.json(docs);
});
});
app.get('/users/:email', function (req, res) {
if (req.params.email) {
User.find({ email: req.params.email }, function (err, docs) {
res.json(docs);
});
}
});
// Start the server
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
当我执行sudo node server.js
命令时,我得到以下信息:
Example app listening at http://0.0.0.0:3000
[Error: failed to connect to [127.0.0.1:27017]]
正如您所看到的,IP地址不正确,localhost:3000(127.0.0.1:3000)也不可用。
有人可以帮助我吗? 谢谢!
答案 0 :(得分:0)
我已将以下行添加到/ etc / network / interfaces中,之后我的网站可用:
auto lo
iface lo inet loopback