Last active
March 1, 2019 03:49
-
-
Save grantcarthew/061f5c81f4a89b9d2185263231147526 to your computer and use it in GitHub Desktop.
medium-app.js
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
//app.js | |
// Constants | |
const MAX_CONTENT_LENGTH_ACCEPTED = 102400000 | |
const COOKIESIGNSECRET = process.env.COOKIESIGNSECRET | |
// Core Modules | |
const express = require('express') | |
const bodyParser = require('body-parser') | |
const cookieParser = require('cookie-parser') | |
// Security Modules | |
const helmet = require('helmet') | |
const hpp = require('hpp') | |
const cors = require('cors') | |
const contentLength = require('express-content-length-validator') | |
// Controllers | |
const controllers = require('./controllers') | |
const controllerAuth = controllers.Auth | |
// Custom middleware | |
const middleware = require('./middleware') | |
const setReqSession = middleware.setReqSession | |
const reqResLog = middleware.reqResLog | |
const errorHandler = middleware.errorHandler | |
// Application Services | |
const app = express() | |
app.enable('trust proxy') | |
// Route Modules | |
const routes = require('./routes') | |
// Security Middleware | |
app.use(helmet()) | |
app.use(hpp()) | |
app.use(contentLength.validateMax({ max: MAX_CONTENT_LENGTH_ACCEPTED })) | |
app.use(cors()) | |
// Custom Middleware Initialization | |
app.use(cookieParser(COOKIESIGNSECRET)) | |
app.use(setReqSession) // cookieParser dependent | |
app.use(reqResLog) | |
app.use(bodyParser.json()) // Extract the data from the body of the request | |
app.use(bodyParser.urlencoded({ extended: true })) | |
// Route Processing | |
app.use('/api', routes) | |
app.get('/', controllerAuth.Redirect) | |
// A standard error handler | |
app.use(errorHandler) | |
module.exports = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment