Skip to content

Instantly share code, notes, and snippets.

@ozanmuyes
Last active October 4, 2018 14:17
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 ozanmuyes/f33638036d1680cb6685c7d6aa150f4f to your computer and use it in GitHub Desktop.
Save ozanmuyes/f33638036d1680cb6685c7d6aa150f4f to your computer and use it in GitHub Desktop.
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