Last active
January 30, 2019 13:44
-
-
Save hypeJunction/32a9d9ff9a8007e7e7bc5cd9c14b13d2 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
import Express from 'express'; | |
import http from 'http'; | |
import Response from './Response'; | |
export default class App { | |
constructor (services) { | |
this.services = services; | |
} | |
start () { | |
const { express, db } = this.services; | |
express.set('services', this.services); | |
// configure top-level express middleware, e.g. bodyParser, cors etc | |
express.use(async (request, response, next) => { | |
const { | |
session, events, | |
} = this.services; | |
events.emit('boot', this.services); | |
// boot any services, e.g. establish database connection, | |
// read request headers to initialize the session etc. | |
events.emit('ready', this.services); | |
// not in the scope of this example, | |
// but there is a similar event emitted from the | |
// last express middleware we use to wrap our route response | |
// into an API response | |
events.emit('request', request); | |
next(); | |
}); | |
// register express routes | |
// register error/response handling middleware | |
process.on('unhandledRejection', (err) => { | |
const { logger } = this.services; | |
logger.error(err); | |
}); | |
return express; | |
} | |
serve () { | |
const { | |
express, | |
config, | |
logger, | |
} = this.services; | |
const port = config.port || 3000; | |
const hostname = config.hostname || 'localhost'; | |
express.set('port', port); | |
express.set('hostname', hostname); | |
const server = http.createServer(express); | |
server.listen({ | |
port, | |
host: hostname, | |
}); | |
server.on('error', (err) => { | |
// handle your errors | |
}); | |
return this; | |
} | |
} |
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
import App from './App'; | |
import services from './services'; | |
const app = new App(services()); | |
app.start(); | |
export default app; |
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
#!/usr/bin/env node | |
import app from './app'; | |
app.serve(); |
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
// And here is the sample app factory, we can use for our tests | |
import Seeder from './seeder'; | |
import Sequelize from 'sequelize'; | |
import nodemailer from 'nodemailer'; | |
import Services from '../../src/services'; | |
import App from '../../src/app/App'; | |
export default async function () { | |
const services = Services(); | |
const connection = new Sequelize('', '', '', { | |
dialect: 'sqlite', | |
logging: false, | |
operatorsAliases: false, | |
define: { | |
underscored: true, | |
}, | |
}); | |
const transport = nodemailer.createTransport({ | |
jsonTransport: true, | |
}); | |
services.register('dbConnection', connection); | |
services.register('seeder', Seeder, ['db']); | |
services.register('mailerTransport', transport); | |
services.register('stripe', {}); | |
services.db.loadModels(services); | |
await services.db.sync({ | |
force: true, | |
}); | |
return new App(services).start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment