express.js 1.28 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const helmet = require('helmet')
const path = require('path')

Yoon, Daeki's avatar
Yoon, Daeki committed
7
8
9
10
11
12
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const loginRouter = require('./routes/login');
const reservesRouter = require('./routes/reserves');
const noticeRouter = require('./routes/notices');
const writesRouter = require('./routes/writes');
Yoon, Daeki's avatar
Yoon, Daeki committed
13
14
15
16
17
18

const CURRENT_WORKING_DIR = process.cwd()
const app = express()

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
Yoon, Daeki's avatar
Yoon, Daeki committed
19
// app.use(helmet())
Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
app.use(cors())

Yoon, Daeki's avatar
Yoon, Daeki committed
22
23
console.log('path=', path.join(CURRENT_WORKING_DIR, 'client', 'build'))
app.use('/', express.static(path.join(CURRENT_WORKING_DIR, 'client', 'build')))
Yoon, Daeki's avatar
Yoon, Daeki committed
24

Yoon, Daeki's avatar
Yoon, Daeki committed
25
26
27
28
29
30
app.use('/', indexRouter);
app.use('/users', usersRouter, reservesRouter);
app.use('/login', loginRouter);
app.use('/reserves', reservesRouter);
app.use('/notices', noticeRouter);
app.use('/writes', writesRouter);
Yoon, Daeki's avatar
Yoon, Daeki committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

app.use((err, req, res, next) => {
  if (err.name === 'UnauthorizedError') {
    res.status(401).json({
      error: err.name + ': ' + err.message
    })
  } else if (err) {
    res.status(400).json({
      error: err.name + ': ' + err.message
    })
    console.log(err)
  }
})

module.exports = app