-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (67 loc) · 2.17 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
/*
* networq Main Server
*/
/** Require environment variable(s) */
require('dotenv').config();
/** Require middlewares */
const express = require('express');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const methodOverride = require('method-override');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const path = require('path');
/** Instantiate server */
const app = express();
const PORT = process.env.PORT || 3000;
/** Use middlewares */
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({ limit: '70mb', extended: true }));
app.use(bodyParser.urlencoded({ limit: '70mb', extended: true }));
app.use(methodOverride('_method'));
app.use(cookieParser());
/** Custom auth-checking middleware */
const checkAuth = (req, res, next) => {
if (typeof req.cookies.faceToken === 'undefined' || req.cookies === null) {
req.user = null;
} else {
const token = req.cookies.faceToken;
const decodedToken = jwt.decode(token, { complete: true }) || {};
req.user = decodedToken.payload;
}
next();
};
/** Custom auth-checking middleware */
const checkUser = (req, res, next) => {
if (req.user){
let currentUser = req.user;
console.log("currentUser", currentUser);
} else {
// console.log("User is not currently logged in!")
// let currentUser = null;
// console.log("currentUser", currentUser);
}
next();
};
/* Use custom middlewares */
app.use(checkAuth);
app.use(checkUser);
/** Database connection */
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/networq', {
useNewUrlParser: true
});
mongoose.set('useCreateIndex', true);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Database connected successfully.');
});
/** Require controller(s) */
require('./controllers/index')(app);
// require('./controllers/auth')(app);
// require('./controllers/users')(app);
/** Port listener */
app.listen(PORT, () => {
console.log('networq app listening on port', PORT);
});
module.exports = app;