-
-
Save vdeturckheim/260cab4819927877f77844917fd11b95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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