-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
69 lines (59 loc) · 1.46 KB
/
index.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
const express = require('express')
const app = express()
const cors = require('cors')
const http = require('http')
const csrf = require('csurf')
const cookieParser = require('cookie-parser')
const server = http.createServer(app)
const promisePool = require('./config/db.js')
const ORIGIN_URL = require('./config/index.js')
const csrfProtection = csrf({
cookie: true,
})
app.use(cookieParser())
app.use(
express.urlencoded({
extended: true,
})
)
app.use(express.json())
app.set('view engine', 'pug')
app.use(
cors({
origin: ORIGIN_URL,
credentials: true,
optionsSuccessStatus: 200,
})
)
app.use(csrfProtection)
app.get('/', async (_, res) => {
try {
const [rows, fields] = await promisePool.query('select 1 as num')
res.render('index', {
title: 'Hey',
message: `Hello there!num is ${rows[0].num}`,
})
} catch (err) {
console.log('error:', err)
res.render('index', {
title: 'Hey',
message: `Hello there!`,
})
}
})
app.use(
'/api/v1',
(() => {
const router = express.Router()
router.use('/login', require('./api/login.js'))
router.use('/logout', require('./api/logout.js'))
router.use('/users', require('./api/users.js'))
router.use('/users/:id', require('./api/users.js'))
router.use('/csrf-token', require('./api/csrfToken.js'))
router.use('/tasks', require('./api/tasks.js'))
return router
})()
)
server.listen(5000, () => {
console.log('listening on *:5000')
})