-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
78 lines (66 loc) · 1.91 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
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
// const env = process.env.NODE_ENV || 'development'
const express = require('express')
// const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const path = require('path')
const config = require('./config/config')
const fs = require('fs')
const indexRouter = require('./routes/index')
const usersRouter = require('./routes/users')
const itemsRouter = require('./routes/items')
const settingsRouter = require('./routes/settings')
const enhancedRouter = require('./routes/enhanced')
const app = express()
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
// app.use(express.static(path.join(__dirname, 'public')))
/**
* Set up and register passport for authentication
*/
const passport = require('passport')
app.use(passport.initialize())
require('./passport')(passport)
/**
* Handle CORS
*/
const cors = require('cors')
app.use(
cors({
// ...(env === 'development' ? { origin: 'http://localhost:8080' } :
origin: true,
credentials: true
})
)
// Create upload directories
for (const subdir of ['avatars', 'icons', 'backgrounds']) {
fs.mkdirSync(path.resolve(path.join(config.uploadDir, subdir)), {
recursive: true
})
}
app.use(express.static(path.join(__dirname, './dist/spa/')))
app.use(express.static(config.uploadDir))
/**
* Authentication middleware to attach the currently logged
* in user to the request
*/
const auth = require('./middleware/auth')
app.use(auth.authorize)
app.use('/', indexRouter)
app.use('/users', usersRouter)
app.use('/items', itemsRouter)
app.use('/settings', settingsRouter)
app.use('/enhanced', enhancedRouter)
// Our own error handler to make sure everything returns as JSON
app.use((err, req, res, next) => {
if (err) {
return res.status(500).json({
status: 'error',
result: err.message
})
}
return next()
})
module.exports = app