forked from TuringCom/backend_challenge_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·93 lines (79 loc) · 2.19 KB
/
Copy pathindex.js
File metadata and controls
executable file
·93 lines (79 loc) · 2.19 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
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
import '@babel/polyfill';
import express from 'express';
import expressWinston from 'express-winston';
import winston from 'winston';
import morgan from 'morgan';
import log from 'fancy-log';
import expressValidator from 'express-validator';
import bodyParser from 'body-parser';
import compression from 'compression';
import helmet from 'helmet';
import cors from 'cors';
import router from './routes';
const isProduction = process.env.NODE_ENV === 'production';
const app = express();
const corsOptions = {
credentials: true,
origin: ['http://localhost:3000'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
app.use(cors(corsOptions));
// compression and header security middleware
app.use(compression());
app.use(helmet());
app.use(morgan('dev'));
app.use(
bodyParser.urlencoded({
limit: '50mb',
extended: true,
})
);
app.use(bodyParser.json());
app.use(expressValidator());
app.use(
expressWinston.logger({
transports: [new winston.transports.Console()],
meta: false,
expressFormat: true,
colorize: true,
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
})
);
app.use('/stripe/charge', express.static(`${__dirname}/public`));
app.use(router);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Resource does not exist');
err.status = 404;
next(err);
});
if (!isProduction) {
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
log(err.stack);
res.status(err.status || 500).json({
error: {
message: err.message,
error: err,
},
status: false,
});
});
}
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
// eslint-disable-line no-unused-vars
return res.status(err.status || 500).json({
error: {
message: err.message,
error: {},
},
status: false,
});
});
// configure port and listen for requests
const port = parseInt(process.env.NODE_ENV === 'test' ? 8378 : process.env.PORT, 10) || 80;
export const server = app.listen(port, () => {
log(`Server is running on http://localhost:${port} `);
});
export default app;