-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
132 lines (105 loc) · 3.26 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import path from "path";
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import express from 'express';
import favicon from 'serve-favicon';
import hbs from 'hbs';
import mongoose from 'mongoose';
import logger from 'morgan';
import cors from 'cors';
import passport from './passport/index.js';
import nodeSaas from 'node-sass-middleware';
import session from "express-session";
import MongoStore from 'connect-mongo';
import flash from "connect-flash";
import debug from 'debug';
// Routes imports
import index from './routes/index.js';
import authRoutes from './routes/auth.js';
import userRoutes from './routes/userProfile.js';
import phenomRoutes from './routes/phenomena.js';
import mapRoutes from './routes/mapRoutes.js';
import apiRoutes from './routes/apiRoutes.js';
import routesRoutes from './routes/routesRoutes.js';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
dotenv.config();
// debug(`wannabelieve: ${path.basename(__filename).split('.')[0]}`);
mongoose.connect(process.env.DBURL, {
useNewUrlParser: true
})
.then(x => {
console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)
})
.catch(err => {
console.error('Error connecting to mongo', err)
});
const app = express();
// Middleware Setup
app.enable("trust proxy");
app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
// Express View engine setup
app.use(nodeSaas({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true,
outputStyle: 'extended',
sourceMap: false
}));
//outputStyle: 'compressed' --> compress style
//debug: true --> to show errors
//indentedSyntax: true --> to read .sass files
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
hbs.registerPartials(__dirname + "/views/partials");
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
hbs.registerHelper('ifUndefined', (value, options) => {
if (arguments.length < 2)
throw new Error("Handlebars Helper ifUndefined needs 1 parameter");
if (typeof value !== undefined) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
hbs.registerHelper('select', function (selected, options) {
return options.fn(this).replace(
new RegExp(' value=\"' + selected + '\"'),
'$& selected="selected"');
});
// default value for title local
app.locals.title = 'wannabelieve';
// Enable authentication using session + passport
app.use(session({
secret: process.env.SECRET,
resave: true,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.DBURL,
ttl: 24 * 60 * 60 // 1 day
})
}))
app.use(flash());
passport(app);
app.use((req, res, next) => {
res.locals.user = req.user;
let messages = [...req.flash('error'), ...req.flash('info')];
//console.log(messages);
res.locals.messages = messages;
next();
});
app.use('/', index);
app.use('/auth', authRoutes);
app.use('/', userRoutes);
app.use('/', phenomRoutes);
app.use('/map', mapRoutes);
app.use('/api', apiRoutes);
app.use('/routes', routesRoutes);
export default app;