generated from wecode-bootcamp-korea/backend-2nd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
41 lines (34 loc) · 923 Bytes
/
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
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const dotenvPath = process.env.DOTENV_CONFIG_PATH;
require('dotenv').config({ path: dotenvPath });
const indexRouter = require('./src/routes');
const app = express();
const createApp = () => {
app.set('port', process.env.PORT || 8000);
app.use(
cors({
origin: '*',
credentials: true,
}),
);
app.use(express.json());
app.use(cors());
app.use(morgan('combined'));
app.use(express.urlencoded({ extended: true }));
app.use(indexRouter);
app.use((req, _, next) => {
const error = new Error(`${req.method} ${req.url} 라우터가 없습니다.`);
error.status = 404;
next(error);
});
app.use((err, _, res, next) => {
res.status(err.status || 500);
return res.json({
error: `${err.message}`,
});
});
return app;
};
module.exports = { createApp };