Skip to content

Instantly share code, notes, and snippets.

@crizstian
Last active January 21, 2017 21:25
Show Gist options
  • Save crizstian/b2c0f299951dc0f91ac2548747f1e7fe to your computer and use it in GitHub Desktop.
Save crizstian/b2c0f299951dc0f91ac2548747f1e7fe to your computer and use it in GitHub Desktop.
Example of express server app
'use strict'
const express = require('express')
const morgan = require('morgan')
const helmet = require('helmet')
const movieAPI = require('../api/movies')
const start = (options) => {
return new Promise((resolve, reject) => {
// we need to verify if we have a repository added and a server port
if (!options.repo) {
reject(new Error('The server must be started with a connected repository'))
}
if (!options.port) {
reject(new Error('The server must be started with an available port'))
}
// let's init a express app, and add some middlewares
const app = express()
app.use(morgan('dev'))
app.use(helmet())
app.use((err, req, res, next) => {
reject(new Error('Something went wrong!, err:' + err))
res.status(500).send('Something went wrong!')
})
// we add our API's to the express app
movieAPI(app, options)
// finally we start the server, and return the newly created server
const server = app.listen(options.port, () => resolve(server))
})
}
module.exports = Object.assign({}, {start})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment