Created
June 18, 2018 09:06
-
-
Save paulbjensen/46f1a4a15127a3739fcdc5350f91b4fc to your computer and use it in GitHub Desktop.
The API repo's index.js file, part of an article on Medium.
This file contains hidden or 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'; | |
/*eslint no-console: ["error", { allow: ["log"] }] */ | |
// Dependencies | |
const express = require('express'); | |
const Raven = require('raven'); | |
const { port, allowedOrigins, sentryDSN } = require('./config'); | |
Raven.config(sentryDSN).install(); | |
const httpShutdown = require('http-shutdown'); | |
const bodyParser = require('body-parser'); | |
const controllers = require('./controllers'); | |
const helmet = require('helmet'); | |
const routes = require('./routes'); | |
// Initialize the app | |
const app = express(); | |
// Middleware | |
app.use(Raven.requestHandler()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
app.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', allowedOrigins.join(',')); | |
res.header('Access-Control-Allow-METHODS', 'GET,POST,PUT,PATCH,DELETE'); | |
res.header( | |
'Access-Control-Allow-Headers', | |
'X-Requested-With,Content-Type,Authorization' | |
); | |
next(); | |
}); | |
app.use(helmet()); | |
// Load the API routes from the routes file | |
routes.load(app, controllers); | |
// Listen on the port | |
const server = httpShutdown(app.listen(port, () => { | |
console.log(`API is listening on port ${port}`); | |
})); | |
server.host = `http://localhost:${port}`; | |
module.exports = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment