Created
August 25, 2018 19:55
-
-
Save greyscaled/e3cae2e7d0390dc32781e19dff15391c to your computer and use it in GitHub Desktop.
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
module.exports = function ({ port }) { | |
const debug = require('debug')('run__on::server::app.js') | |
const express = require('express') | |
const path = require('path') | |
const helmet = require('helmet') | |
const RateLimit = require('express-rate-limit') | |
const { internalError } = require('./middleware') | |
// Configuration ----------------/ | |
debug('configuring express application') | |
const app = express() | |
// set the port | |
if (port) { | |
debug('setting port to %d', port) | |
app.set('port', port) | |
} | |
// res.render calls use pug templates in views directory | |
app.set('view engine', 'pug') | |
app.set('views', path.join(__dirname, 'views')) | |
// for use behind Heroku proxy | |
app.enable('trust proxy') | |
// PRE-ROUTE --------------------/ | |
// adds basic security headers | |
app.use(helmet()) | |
// request rate limited | |
app.use(new RateLimit({ | |
// 10 minutes | |
windowMs: 10 * 60 * 1000, | |
// 1000 requests per 10 mins | |
max: 1000, | |
delayMs: 0 | |
})) | |
// ROUTING ----------------------/ | |
require('./routes')(app) | |
app.get('*', (req, res, next) => { | |
next() | |
}) | |
app.use((req, res, next) => { | |
res.send('foo') | |
}) | |
// POST-ROUTE -------------------/ | |
app.use(internalError) | |
return app | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment