I have installed Cubian OS on my Cubieboard . Then I installed node.js and mongoDB using this tutorial . It seems node.js is working properly. I can start mongod service but I have several problems:
npm install
produce a lots of errors and I can't install required modules using this command (but I still can use nmp install module_name
command) My test node.js code:
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)
});
When I execute sudo node server.js
command I get following:
Example app listening at http://0.0.0.0:3000
[Error: failed to connect to [127.0.0.1:27017]]
As you can see the IP address is incorrect and localhost:3000 (127.0.0.1:3000) is unavailable too.
Does anyone can help me? Thanks!
I've added the following lines to /etc/network/interfaces and after that my web-site became available:
auto lo
iface lo inet loopback
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.