-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·106 lines (94 loc) · 2.99 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const dotenv = require('dotenv');
dotenv.config();
const path = require('path');
const express = require('express');
const createError = require('http-errors');
const connectMongo = require('connect-mongo');
const expressSession = require('express-session');
const logger = require('morgan');
const passport = require('passport');
const sassMiddleware = require('node-sass-middleware');
const serveFavicon = require('serve-favicon');
const mongoose = require('mongoose');
const passportConfigure = require('./passport-configuration.js');
const baseRouter = require('./routes/base');
const authenticationRouter = require('./routes/authentication');
const domainRouter = require('./routes/domainRoutes');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(serveFavicon(path.join(__dirname, 'public/images', 'favicon.ico')));
app.use(
sassMiddleware({
src: path.join('styles'),
dest: path.join(__dirname, 'public/styles'),
prefix: '/styles',
outputStyle:
process.env.NODE_ENV === 'development' ? 'expanded' : 'compressed',
force: process.env.NODE_ENV === 'development',
sourceMap: process.env.NODE_ENV === 'development'
})
);
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'));
app.use(express.urlencoded({ extended: true }));
app.use(
expressSession({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: false,
proxy: true,
cookie: {
maxAge: 15 * 24 * 60 * 60 * 1000,
httpOnly: true
},
store: connectMongo.create({
mongoUrl: process.env.MONGODB_URI,
ttl: 60 * 60
})
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());
app.use('/', baseRouter);
app.use('/authentication', authenticationRouter);
app.use('/api/domains', domainRouter);
// Catch missing routes and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// Catch all error handler
app.use((error, req, res, next) => {
// Set error information, with stack only available in development
res.locals.message = error.message;
res.locals.error = req.app.get('env') === 'development' ? error : {};
res.status(error.status || 500);
res.render('error');
});
const { NODE_ENV, PORT, MONGODB_URI } = process.env;
mongoose
.connect(MONGODB_URI)
.then(() => {
console.log(`Database connected to URI "${MONGODB_URI}"`);
app
.listen(Number(PORT), () => {
console.log(`Server listening to requests on port ${PORT}`);
if (NODE_ENV === 'development') {
console.log(`Visit http://localhost:${PORT} to develop your app`);
}
})
.on('error', (error) => {
console.log('There was a server error.', error);
process.exit(1);
});
})
.catch((error) => {
console.log(
`There was an error connecting to the database "${MONGODB_URI}"`,
error
);
});
module.exports = app;
module.exports.db = mongoose.connection