-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (80 loc) · 2.33 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
require('dotenv').config();
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
// Routes
const authRouter = require('./routes/auth');
const usersRouter = require('./routes/users');
const issuesRouter = require('./routes/issues');
const projectsRouter = require('./routes/projects');
const chartsRouter = require('./routes/charts');
// Open database connection
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
keepAlive: true,
reconnectTries: Number.MAX_VALUE
})
.then(x => {
console.log(`Connected to MongoDB! Database name: '${x.connections[0].name}"`);
})
.catch(err => {
console.error('Error connecting to MongoDB', err);
});
const app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Allow frontend access to API (CORS)
app.use(
cors({
credentials: true,
origin: [process.env.PUBLIC_DOMAIN]
})
);
// Session middleware
app.use(
session({
store: new MongoStore({
mongooseConnection: mongoose.connection,
ttl: 24 * 60 * 60 // 1 day
}),
secret: process.env.SECRET_SESSION,
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000
}
})
);
// Routes
app.use('/api/auth', authRouter);
app.use('/api/users', usersRouter);
app.use('/api/issues', issuesRouter);
app.use('/api/projects', projectsRouter);
app.use('/api/charts', chartsRouter);
// Catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// Error handler
app.use((err, req, res, next) => {
// Set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// Respond with error message
res.status(err.status).json(err.message);
});
module.exports = app;