-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (67 loc) · 2.23 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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const createError = require('http-errors');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const cors = require('cors');
//Config imports
require('./configs/db.config');
require('./configs/passport.config').setup(passport);
const corsConfig = require('./configs/cors.config');
//Routers
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users.route');
const sessionsRouter = require('./routes/sessions.route');
const vehiclesRouter = require('./routes/vehicles.route');
const commutesRouter = require('./routes/commutes.route');
const scoresRouter = require('./routes/scores.route');
const chatRouter = require('./routes/chat.route');
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')));
app.use(cors(corsConfig));
app.use(session({
secret: process.env.COOKIE_SECRET || 'Super Secret',
resave: true,
saveUninitialized: true,
cookie: {
httpOnly: true,
maxAge: 1367800020
}
}));
app.use(passport.initialize());
app.use(passport.session());
//Routing
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/sessions', sessionsRouter);
app.use('/commutes/', commutesRouter);
app.use('/scores', scoresRouter);
app.use('/users/:authId/messages', chatRouter);
app.use('/users/:userId/vehicles', vehiclesRouter);
app.use(function (req, res, next) {
next(createError(404));
});
app.use(function (error, req, res, next) {
console.error(error);
res.status(error.status || 500);
const data = {}
if (error instanceof mongoose.Error.ValidationError) {
res.status(400);
for (field of Object.keys(error.errors)) {
error.errors[field] = error.errors[field].message
}
data.errors = error.errors
} else if (error instanceof mongoose.Error.CastError) {
error = createError(404, 'Resource not found')
}
data.message = error.message;
res.json(data);
});
module.exports = app;