This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (62 loc) · 2.5 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
78
79
80
81
const express = require('express')
const app = express()
const bodyParser = require("body-parser")
const passport = require("passport")
const session = require('express-session');
const path = require("path")
const fs = require("fs")
const shortid = require('shortid');
require('dotenv').config(); //Load Environment variables from .env
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Opened connection to mongo server.")
});
require("./app/passport")(passport)
app.use(session({ secret: 'supersecretsaucemakescryptostronger' })); // session secret
app.use(passport.initialize());
app.use(passport.session({
cookie: { maxAge: 60000 },
rolling: true,
resave: true,
saveUninitialized: false})); // persistent login sessions
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended:true, limit: '5mb'}))
app.use(bodyParser.json({limit: '5mb'}))
// Realistically, all cards should have the id's that were generated when they were added.
// However, for example in the case of manual db modification, the server should check that everything has an id. Else stuff breaks!
/*var cards = db.get("cards").value();
for (var i = 0; i < cards.length; i++){
if (cards[i].id == undefined)
cards[i].id = shortid.generate();
}
db.get("cards").merge(cards).write();
cards = undefined;
var terms = db.get("notes").get("glossary").value();
for (var i = 0; i < terms.length; i++){
if (terms[i].id == undefined)
terms[i].id = shortid.generate();
}
db.get("glossary").merge(terms).write();
*/
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
//res.redirect('/');
res.status(401).json({error:"Auth failed or expired"})
}
var cards = require('./app/routes/cards.js')(db,passport)
//var studysheets = require('.app//routes/studysheets.js')(db)
var hierarchy = require('./app/routes/hierarchy.js')(db,passport)
//var glossary = require('./app/routes/glossary.js')(db)
app.use("/cards", isLoggedIn, cards)
app.use(require("./app/routes/forgot")())
require("./app/routes/authentication")(app,passport,db)
//app.use("/studysheets",studysheets)
//app.use("/glossary",glossary)
//app.use("/hierarchy",hierarchy)
app.listen(8000, () => console.log('Study app active. Port 8000.'))