-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (49 loc) · 1.52 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
//MODULE IMPORTS
const express = require('express');
const morgan = require('morgan'); //Logger
const path = require('path');
const mongoose = require('mongoose');
const dbconfig = require('./dbconfig'); //Database config
const cookieParser = require('cookie-parser');
const session = require('express-session');
//ROUTER IMPORTS
const indexRouter = require('./routes/index');
const userRouter = require('./routes/users');
//PASSPORT FOR AUTHENTICATION
const passport = require('passport');
let app = express();
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended:false }));
app.use(session({
name : 'session-id',
secret : '12345-67890-11346-44003-24354',
saveUninitialized : false,
resave : false
}));
app.use(passport.initialize());
app.use(passport.session());
//PATHS ROUTER
app.use('/',indexRouter);
app.use('/users',userRouter);
app.use(express.static(__dirname + '/public'));
//CONNECT TO MONGODB
const url = dbconfig.mongoUrl;
const connect = mongoose.connect(url, { useNewUrlParser:true , useUnifiedTopology:true });
connect.then(() => {
console.log('Connected to mongoDB server correctly.');
}, (err) => console.log(err));
//ERROR HANDLER
app.use(function (err, req, res, next) {
if (err) {
let status = 500;
if (err.status >= 400 || err.status < 600) {
status = err.status;
}
res.status(status).send(`Something went wrong. Description of error: ${err.message} .`);
}
else {
next();
}
});
module.exports = app;