-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
143 lines (113 loc) · 2.95 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
global.__DEV__ = process.env.NODE_ENV === 'development';
const fs = require('fs');
const path = require('path');
const join = path.join;
const koa = require('koa');
const cors = require('@koa/cors');
const app = new koa();
const helmet = require('koa-helmet');
const Router = require('koa-router');
const serve = require('koa-static');
const bunyan = require('bunyan');
const uuid = require('uuid/v4');
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const compress = require('koa-compress');
const bodyParser = require('koa-bodyparser');
require('events').EventEmitter.defaultMaxListeners = Infinity;
process.on('unhandledRejection', err => {
console.error(err);
log.error(err);
process.exit(1);
});
process.on('uncaughtException', err => {
console.error(err);
log.error(err);
process.exit(1);
});
app.use(cors({
allowMethods: ['GET', 'POST'],
origin: null
}));
app.use(helmet());
app.use(async (ctx, next) => {
let url = path.normalize(ctx.request.url);
if (url.endsWith(`.html`) || url == '/') return await next();
return serve(join(process.cwd(), 'dist'), {
maxage : 86400000*30,
gzip: true,
usePrecompiledGzip: true
})(ctx, next);
});
let log = bunyan.createLogger({
name: 'APP',
requestId: uuid(),
streams: [
{
level: 'error',
path: join(process.cwd(), './logs/errors.log')
}
]
});
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
log.error(err);
if (__DEV__) {
console.error(err);
} else {
ctx.status = ctx.status > 500 ? 500 : ctx.status;
ctx.body = 'Server error';
}
}
});
app.use(bodyParser({ // application/json , application/x-www-form-urlencoded ONLY
formLimit: '1mb',
jsonLimit: '1mb'
}));
app.use(conditional());
app.use(etag());
/**
ROUTES
**/
const router = new Router();
router.post('/submit', async ctx => {
const { cvv, amount, cardHolder, cardNumber, expireYear, expireMonth, countryCode } = ctx.request.body;
ctx.type = 'json';
if (!cvv || (123 == cvv)) {
ctx.status = 400;
return ctx.body = {
message: [{
field: 'cvv',
message: 'Bad CVV'
}]
};
}
ctx.body = { message: 'success' };
});
router.get('/', async ctx => {
ctx.type = 'html';
ctx.body = await new Promise((resolve, reject) => {
fs.readFile('./dist/index.html', (err, html) => {
if (err) return reject(err);
resolve(html);
// resolve(html.toString('utf-8').replace('#!csrf', ctx.csrf));
});
});
});
router.get('/demo', async ctx => {
ctx.type = 'html';
ctx.body = await new Promise((resolve, reject) => {
fs.readFile('./dist/index.html', (err, html) => {
if (err) return reject(err);
resolve(html);
// resolve(html.toString('utf-8').replace('#!csrf', ctx.csrf));
});
});
});
app.use(router.routes());
const server = app.listen(3000);
console.log('SERVER LISTENING ON PORT: 3000');
module.exports = server;