Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Last active November 21, 2017 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/260cab4819927877f77844917fd11b95 to your computer and use it in GitHub Desktop.
Save vdeturckheim/260cab4819927877f77844917fd11b95 to your computer and use it in GitHub Desktop.
'use strict';
const Express = require('express');
const Mongoose = require('mongoose');
const app = Express();
Mongoose.connect('mongodb://localhost/test', { useMongoClient: true });
Mongoose.Promise = global.Promise;
const Cat = Mongoose.model('Cat', { name: String });
app.get('/cats', async (req, res, next) => {
// this route simply list the content of the 'Cat' collection
try {
const cats = await Cat.find().exec(); // using await syntax make it much simpler here
return res.json(cats);
}
catch (e) {
return next(e);
}
});
app.listen(9090, () => {
console.log('server running on port 9090');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment