-
Notifications
You must be signed in to change notification settings - Fork 6
/
app-server.js
47 lines (41 loc) · 1.61 KB
/
app-server.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
const express = require('express')
const app = express()
const path = require('path') // enables us to serve unix/windows w/o having to write multiple paths
const logger = require('morgan')
const { v2 } = require('cloudinary')
const cloudinary = v2
const fileUpload = require('express-fileupload')
const favicon = require('serve-favicon')
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(fileUpload({
limits: { fileSize: 40 * 1024 * 1024 }
}))
cloudinary.config({
cloud_name: process.env.cloud_name,
api_key: process.env.api_key,
api_secret: process.env.api_secret
})
app.use((req, res, next) => {
res.locals.data = {}
next()
})
app.use(express.static(path.join(__dirname, 'public')))
app.use(logger('dev'))
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use(require('./config/checkToken'))
app.use(require('./config/checkBodyToken'))
// Put API routes here, before the "catch all" route
app.use('/api/users', require('./routes/api/users'))
// Protect the API routes below from anonymous users
const ensureLoggedIn = require('./config/ensureLoggedIn')
app.use('/api/items', ensureLoggedIn, require('./routes/api/items'))
app.use('/api/orders', ensureLoggedIn, require('./routes/api/orders'))
app.use('/api/reviews', ensureLoggedIn, require('./routes/api/reviews'))
app.use('/api/shops', ensureLoggedIn, require('./routes/api/shops'))
app.use('/api/users', ensureLoggedIn, require('./routes/api/users'))
// catch all -> if url doesn't match with any routes; for react router
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
module.exports = app