I am building a REST api service with Node, Express and MongoDB. I installed MongoDB and it runs normally on my PC on localhost:27017. I can add collections and read em. In my app.js file I have this setup
var express = require('express'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); mongoose.connect('mongodb://127.0.0.1:27017/bookAPI'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { console.log("h"); }); var app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); var port = process.env.PORT || 3000; app.use('/api', require('./routes/api.js')); app.listen(port, function(){ console.log('Running on port ' + port); });
I always get an error- MongoError - cannot connect UNKNOWN I searched for hours and didn't find any solution. How can I fix it so it can connect to MongoDB, which is working properly...?