-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (42 loc) · 1.11 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
require('./lib/getSettings');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const throwError = require('./lib/throwError');
const getRoutes = require('./lib/getRoutes');
const routes = getRoutes();
app.use(
cors({
origin:
process.env.NODE_ENV === 'development' ? '*' : 'https://surfspace.me'
})
);
app.use(bodyParser.json({ extended: true }));
routes.forEach(data => {
app.use(data.path || '/', data.router);
});
// 404 처리 핸들러
app.use(() => {
// throwError를 통한 에러 핸들링 권장.
throwError('Page Not found.', 404);
});
// Error 처리 핸들러
//eslint-disable-next-line
app.use((error, req, res, next) => {
const status = error.status || 500;
const message =
error.message && error.expose
? error.message
: 'An error has occurred. Please Try Again.';
const data = error.data || {};
if (!error.expose || process.env.NODE_ENV === 'development') {
console.error(error);
}
res.status(status).json({
status,
message,
...data
});
});
module.exports = app;