-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (66 loc) · 1.8 KB
/
server.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
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const cors = require('cors')
const morgan = require('morgan')
const fs = require('fs')
const session = require('express-session');
const config = require('./config/Config');
const passport = require('passport');
const serveStatic = require('serve-static');
const history = require('connect-history-api-fallback');
const app = express()
const router = express.Router()
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(cors())
app.use(session ({
secret: config.SECRET,
resave: true,
saveUninitialized: true,
cookie: { httpOnly: false },
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect(config.DB,
() => {
console.log('Connection has been made');
})
.catch(err => {
console.error('App starting error:', err.stack);
process.exit(1);
});
// Include controllers
fs.readdirSync("controllers").forEach(function (file) {
if(file.substr(-3) == ".js") {
const route = require("./controllers/" + file)
route.controller(app)
}
})
app.use(history());
app.use(serveStatic(__dirname + '/dist'));
router.get('/api/current_user', isLoggedIn, (req, res) => {
if(req.user) {
console.log(req.user);
res.send({ current_user: req.user })
} else {
console.log('NO')
res.status(403).send({ success: false, msg: 'Unauthorised.' });
}
})
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
console.log('error, auth failed!')
}
router.get('/api/logout', (req, res) => {
req.logout();
res.send();
})
const port = process.env.API_PORT || 8081;
app.use('/', router);
const server = app.listen(port, () => {
console.log(`api running on port ${port}`);
});
module.exports = server;