Reputation: 53
I have this as a server.js:
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Task = require('./api/models/toDoListModel'),
bodyParser = require('body-parser');
mongoose.Promise = global.Promise;
mongoose.createConnection('mongodb://localhost:27017/Tododb');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var routes = require('./api/routes/toDoListRoutes');
routes(app);
app.listen(port);
And when I use posman to post/get, I get no response from the server (on a correct key-value) although localhost:27017 is running and I created a Tododb db using Robo 3T. But when I don't enter any key/value in postman, I get a response. Am I missing something? All addresses are correct.
Upvotes: 0
Views: 1159
Reputation: 1078
You can connect to MongoDB with the mongoose.connect() method.
mongoose.connect('mongodb://localhost:27017/Tododb');
If you don't call
mongoose.connect()
then mongoose.connection
doesn't contain an an open connection. You should be using the return value from your mongo.createConnection()
call instead (that you've to saved into db var and use that object).
Upvotes: 2