Last active
October 4, 2018 14:17
-
-
Save ozanmuyes/f33638036d1680cb6685c7d6aa150f4f 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
const EventEmitter = require('events'); | |
const Koa = require('koa'); | |
const mongoose = require('mongoose'); | |
// Excerpted from https://nodejs.org/dist/latest-v8.x/docs/api/events.html#events_events | |
class MyEmitter extends EventEmitter {} | |
const myEmitter = new MyEmitter(); | |
// Excerpted from https://mongoosejs.com/docs/index.html | |
mongoose.connect('mongodb://localhost/test'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function() { | |
// we're connected! | |
const Role = mongoose.model('Role', { | |
name: String, | |
rules: Object, | |
}); | |
myEmitter.emit('loadAbilities'); | |
}); | |
const abilities = {}; | |
myEmitter.once('loadAbilities', () => { | |
// TODO Load abilities and emit 'startListening' | |
const roles = mongoose.models.Role.find({}); | |
roles.forEach((role) => { | |
abilities[role.name] = role.rules; | |
}); | |
}); | |
// Excerpted from https://koajs.com/#application | |
const app = new Koa(); | |
app.use(async (ctx) => { | |
ctx.body = 'Hello World'; | |
}); | |
myEmitter.once('startListening', () => { | |
app.listen(3000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment