This is my server.js.
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
CohortController =require('./CohortController');
var MongoURL='mongodb://username:password@ds037395.mongolab.com:37395/abc';
mongoose.connect(MongoURL,function (error) {
if (error)
console.error(error);
else
console.log('mongo connected');
});
app.use(bodyParser());
app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);
app.listen(3000,function(){
console.log('Listening...');
})
And my CohortController.js
var mongoose=require('mongoose');
var Cohort=mongoose.model('Cohorts',new Schema({
id:Number,
name:String
}));
module.exports.create=function(req,res){
var cohort=new Cohort(req.body);
cohort.save(function(err,result){
res.json(result);
});
}
module.exports.list=
function(req,res){
Cohort.find({},function(err,results){
res.json(results);
});
}
When I run the node server and use the URL
localhost:3000/api/cohorts
I am getting null JSON i.e []
but when I connect it with the local mongodb instance I get the correct JSON.