Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Created January 17, 2019 21:19
Show Gist options
  • Save bmorrisondev/266d0656f87437c34618ed01a41a3659 to your computer and use it in GitHub Desktop.
Save bmorrisondev/266d0656f87437c34618ed01a41a3659 to your computer and use it in GitHub Desktop.
A simple shell of an Express app
if(process.env.ENVIRONMENT != "PROD") {
require('dotenv').config()
}
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan')
const routes = require('./routes')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(morgan('tiny'))
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, DELETE")
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Approve response to pre-flight requests
if('OPTIONS' == req.method) {
res.send(200);
} else {
next()
}
})
app.use('/', routes)
const port = 3000
app.listen(port)
console.log(`listening on http://localhost:${port}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment