I am writing a simple post call. My code is as follows:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o');
router.use(function(req, res, next) {
console.log('Something is happening.');
next();
});enter code here
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
var Bear = require('./app/models/bears');
router.route('/bears')
.post(function(req, res) {
var bear = new Bear();
bear.name = req.body.name;
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear created!' });
});
});
app.use('/api', router);
app.listen(port);
console.log('Magic happens on port ' + port);
I was previously getting the following error
failed to connect to [novus.modulusmongo.net:27017]
which I rectified by updating mongoose but now still i am getting another error as:
/node_modules/mongoose/node_modules/mongodb/lib/server.js:242
process.nextTick(function() { throw err; })
^
Error: connect ETIMEDOUT 54.209.84.209:27017
Tried with the below sample
var mg= require('mongoose');
var db = mg.connection;
mg.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o');
db.on('open', function (ref) {
console.log('open connection to mongo server.');
});
db.on('connected', function (ref) {
console.log('Connected connection to mongo server.');
});
db.on('disconnected', function (ref) {
console.log(ref);
console.log('disconnected connection.');
});
db.on('close', function (ref) {
console.log('close connection.');
});
db.on('error', function (ref) {
console.log(ref);
console.log('Error connection.');
});
db.on('reconnected', function () {
console.log('MongoDB reconnected!');
});
Below is the error i got.
{ MongoError: failed to connect to server [novus.modulusmongo.net:27017] on first connect
at Pool.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\server.js:313:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:260:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:162:49)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoError',
message: 'failed to connect to server [novus.modulusmongo.net:27017] on first connect' }
So i feel essentially its just the connection string or port or username or password is incorrect. try connecting with above code by simply changing the connection string, port , username or password..
Remember 27017 is not the default port always specially on hosted server
try mlab and see if it connects its free for testing and get your connection string here https://mlab.com/ to see if all your code is working perfectly
Hope that helps you