简体   繁体   中英

node.js - newbie mongojs issue

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

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM