forked from cranebaes/gitbud
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
68 lines (62 loc) · 2.12 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
/*
* ENTRY POINT TO ALL THE SERVER SIDE CODE
*
* Most of the server code is clearly modularised, so this
* is mostly uncontroversial requires and uses.
*
* The other server modules are:
* request-handler
* --Sends correct response to each URL (mostly by calling appropriate function from routes)
* routes
* --Contains functions for generating responses (all called from various points in request-handler)
* authentication
* --Implements passport's GitHub strategy and exports its middleware for use in other modules in order to keep similar code together
* profiling
* --Builds a profile of user's experience by scraping data from GitHub and builds relationships in the db
* by which to sort users in lists.
*/
// Allows storing of environment variables
// in .env of root directory.
require('dotenv').config();
const open = require('open');
// Libraries for handling requests
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
// Libraries for authentication and sessions
const session = require('express-session');
// GitPal modules
const passport = require('./server/authentication').passport;
const requestHandler = require('./server/request-handler');
const eventSockets = require('./server/sockets');
const socketio = require('socket.io');
// Make express server
const app = express();
const port = process.env.PORT || 8080;
const server = app.listen(port, err => {
if (err) {
console.log(err);
} else {
open(`http://localhost:${port}`);
}
});
const io = socketio(server);
eventSockets(io);
// Save sessions
// NOTE: This is using a bad memory store
// https://www.npmjs.com/package/express-session#sessionoptions
app.use(
session({
secret: 'This is a secret',
resave: false,
saveUninitialized: true
})
);
// Set server to use initialized passport from authentication module
app.use(passport.initialize());
app.use(passport.session());
// Serve static files
app.use(express.static(path.join(__dirname, 'dist')));
// All other enpoints routed in request-handler module
app.use(bodyParser.json());
app.use(requestHandler.handler);