-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (54 loc) · 1.62 KB
/
server.js
File metadata and controls
61 lines (54 loc) · 1.62 KB
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
const path = require('path');
const Koa = require('koa');
const rateLimit = require('koa2-rate-limit').rateLimit;
const cors = require('kcors');
const pug = require('js-koa-pug');
const mongoose = require('mongoose');
const { cookieParser } = require('./middleware/cookie');
const router = require('./routes');
const app = new Koa();
mongoose.Promise = global.Promise;
// Start up Mongoose
mongoose.connect(process.env.DATABASE || 'mongodb://localhost:27017/pick');
mongoose.connection.on('error', (err) => {
console.error(`Mongoose error: ${err.message}`);
});
// Add middleware
app
// Log all requests with response time
.use(async (ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method}: ${ctx.url || ''} - ${ms}ms`);
})
// Enforce rate limit
.use(rateLimit({
routes:
[
{ method: 'POST', path: /\/.+\/vote/i },
],
interval: parseInt(process.env.RATELIMIT, 10) || 10000,
max: 2,
}))
// Parse cookies
.use(cookieParser)
// Serve static files
.use(require('koa-static')(path.join(__dirname, 'public')))
// Load pug (with cache enabled in prod)
.use(pug('views', { cache: (process.env.NODE_ENV === 'production') }))
// Load routes
.use(router.routes())
.use(router.allowedMethods())
// CORS
.use(cors);
// Log development errors
app.on('error', (err, ctx) => console.error('server error', err, ctx || null));
// TODO log production errors
// Start server
if (!module.parent) {
const port = process.env.PORT || 3000;
app.listen(port);
console.log(`Koa is listening on port ${port}`);
}
module.exports = app;