-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (61 loc) · 2.13 KB
/
index.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
const http = require('http')
const express = require('express');
const bodyParser = require('body-parser');
const flash = require('connect-flash');
const session = require('express-session')
const passport = require('passport')
const fetch = require('node-fetch');
const handleError = require('./helpers/error')
const { sessionData, socketconn } = require('./socket/socket')
const keys = require('./helpers/keys');
// require('express-async-errors');
const app = express()
server = http.createServer(app);
socketconn.init(server)
// Check if DB can connect and Quit if not// replace with db connection
// fetch('http://localhost:3000/users')
fetch(`${keys.DBCONN}/users`)
.then(console.log('DB Connected'))
.catch(err=>{
console.log({msg:'App cannot connect to database', Error: err});
process.exit(1);
})
// require passport helper module
require('./helpers/passport')(passport)
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(__dirname + '/public'))
// Use sessions. Note. remove session from socket file
app.use(session(sessionData))
// passport middleware
app.use(passport.initialize());
app.use(passport.session())
// connect flash middleware
app.use(flash())
// Message Global Var
app.use((req, res, next)=>{
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.session_error = req.flash('error'); // from passport
next()
})
app.use('/', require('./routes/users'))
app.use('/users', require('./routes/index'))
// default error handling
app.use((req, res, next) => {
res.status(404).send({
status: 404,
error: 'Not found'
})
})
// app.use(methodOverride())
app.use(function (err, req, res, next) {
console.error(err.stack);
handleError(err, res);
// res.status(500).send('Something went wrong!!');
});
const PORT = process.env.PORT || 5000
console.log('database connection', `${keys.DBCONN}`)
server.listen(PORT, ()=>{
console.log(`server is listening on port ${PORT}`)
});