-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (68 loc) · 2.71 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const RateLimit = require('express-rate-limit');
const mustache = require('mustache-express');
const api = require('./routes/api');
const app = express();
// Register '.mustache' extension with The Mustache Express
app.engine('mustache', mustache());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.disable('x-powered-by'); // we don't need the x-powered-by express header
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
// allow larger file uploads: http://stackoverflow.com/a/19965089/2487925
// should match value in nginx.conf on server. for dokku, see example in docs:
// http://dokku.viewdocs.io/dokku/configuration/nginx/#customizing-via-configuration-files-included-by-the-default-tem
const sizeLimit = '25mb';
app.use(bodyParser.json({ limit: sizeLimit }));
app.use(bodyParser.urlencoded({ limit: sizeLimit, extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'static')));
app.use('/', api);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.error(err);
next();
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
next();
});
// rate limiting ===============================================================
if (process.env.NODE_ENV === 'production') {
const apiLimiter = new RateLimit({
windowMs: 10*60*1000, // 10 minute window
delayAfter: 20, // begin slowing down responses after the first 20 requests
delayMs: 0.1*1000, // slow down subsequent responses by 0.1 seconds per request
max: 40, // start blocking after 40 requests
message: 'Too many requests from this IP, please try again after an hour.'
});
app.use('/', apiLimiter);
const uploadLimiter = new RateLimit({
windowMs: 60*60*1000, // 1 hour window
delayAfter: 5, // begin slowing down responses after the first 10 requests
delayMs: 1*1000, // slow down subsequent responses by 1 second per request
max: 10, // start blocking after 10 requests
message: 'Too many requests from this IP, please try again after an hour.'
});
app.use('/upload', uploadLimiter);
}
module.exports = app;