0

I am trying to learn MongoJS, But its not working.
I wrote this code so far -

/* Basics */

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    db = require("mongojs").connect("mydb", ["users"]);

server.listen(27017, null);

io.set('transports', ['xhr-polling']);

// routing
app.get('/', function (req, res) {
    res.sendfile("index.html");
    app.use(express.static(__dirname));
});

db.users.save({username : "admin"}, function(err, saved) {
    if( err || !saved ) { console.log("User not saved"); }
    else { console.log("User saved"); }
});

It log to the console "User not saved", but why? what I did wrong?


Thanks in Advance

julian
  • 4,634
  • 10
  • 42
  • 59

1 Answers1

2

You are confused about ports and are trying to tell your web server to listen on port 27017, which is the port mongodb listens on. Try server.listen(3000) (or another available port of your choosing). If you want to troubleshoot the db.users.save error, try printing the actually error message (err) to console and go from there.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thank you! Now I log the error message `err` and it logs `Error: failed to connect to 127.0.0.1:27017` What I did wrong now? – julian May 22 '13 at 14:02
  • No problem I fixed it.. I just needed to run mongo.exe..I Upvoted you and accepted your answer ;) – julian May 22 '13 at 14:06