-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
92 lines (73 loc) · 2.27 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const passport = require('passport');
require('./config/passport');
// require ('dotenv').config();
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const debug = require('debug');
const cors = require('cors');
const csrf = require('csurf');
require('./models/User.js');
require('./models/Room.js');
require('./models/Review.js');
const path = require('path');
const usersRouter = require('./routes/api/users');
const csrfRouter = require('./routes/api/csrf');
const roomsRouter = require('./routes/api/rooms');
const reviewsRouter = require('./routes/api/reviews');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
const { isProduction } = require('./config/keys')
app.use(passport.initialize());
app.use(
csrf({
cookie: {
secure: isProduction,
sameSite: isProduction && "Lax",
httpOnly: true
}
})
)
app.use('/api/users', usersRouter);
app.use('/api/csrf', csrfRouter);
app.use('/api/rooms', roomsRouter);
app.use('/api/reviews', reviewsRouter);
if (isProduction) {
const path = require('path');
// Serve the frontend's index.html file at the root route
app.get('/', (req, res) => {
res.cookie('CSRF-TOKEN', req.csrfToken());
res.sendFile(
path.resolve(__dirname, './frontend', 'build', 'index.html')
);
});
// Serve the static assets in the frontend's build folder
app.use(express.static(path.resolve("./frontend/build")));
// Serve the frontend's index.html file at all other routes NOT starting with /api
app.get(/^(?!\/?api).*/, (req, res) => {
res.cookie('CSRF-TOKEN', req.csrfToken());
res.sendFile(
path.resolve(__dirname, './frontend', 'build', 'index.html')
);
});
}
app.use((req, res, next) => {
const err = new Error('Not Found');
err.statusCode = 404;
next(err);
})
const serverErrorLogger = debug('backend:error');
app.use((err, req, res, next) => {
serverErrorLogger(err);
const statusCode = err.statusCode || 500
res.status(statusCode);
res.json({
message: err.message,
statusCode,
errors: err.errors
})
})
module.exports = app;