This repository has been archived by the owner on Dec 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
121 lines (102 loc) 路 3.86 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// source: https://github.com/uwmadisonieee/Server-And-Database-Workshop
//-------------------------Module "Importing"-----------------------------//
//modules required (same idea of #includes or Imports)
const express = require("express"); //used as routing framework
const path = require("path"); //Node.js module used for getting path of file
const logger = require("morgan"); //used to log in console window all request
const cookieParser = require("cookie-parser"); //Parse Cookie header and populate req.cookies
const bodyParser = require("body-parser"); //allows the use of req.body in POST request
const http = require('http');
// const jwt = require('jsonwebtoken');
const app = express(); //creates an instance of express
const server = http.createServer(app); //creates an HTTP server instance
require('express-ws')(app);
const { router: api } = require("./routes/api/api.js"); //gets api logic from path
const { router: user } = require("./routes/user/index.js"); //gets user logic from path
const { router: match } = require("./routes/match/matchapi.js"); //gets match logic from path
const config = require('./config/database');
require('dotenv').config()
//-------------------------Express JS configs-----------------------------//
app.use(logger('dev')); //debugs logs in terminal
// IMPORTANT: If you don't use bodyParser then you will NOT be able to call req.body.value
// without parsing JSON yourself
app.use(bodyParser.json()); //parses json and sets to body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use('/api', (req, res, next) => {
// // check header or url parameters or post parameters for token
// // console.log(req.headers)
// const token = req.body.token || req.query.token || req.headers['x-access-token'] || req.body.passwordHash;
// if (!token)
// return res.status(401).send({ error: 'No token provided.', success: 'false' });
// // return res.redirect(403, '/login');
// jwt.verify(token, config.secret, (err, decoded) => {
// if (err)
// return res.status(403).send({ success: false, error: 'Failed to authenticate token.' });
// req.decoded = decoded;
// req.userId = decoded.id;
// next();
// });
// }, api);
app.use('/user', user);
app.use('/match', match);
// so when people try to access it via browser
app.get("/", function(req, res) {
res.status(200).sendFile(path.join(__dirname + "/public/index.html"));
});
const port = normalizePort(process.env.PORT || '8000');
app.set('port', port);
app.use(express.static(__dirname + "/public"));
/**
* Listen on provided port, on all network interfaces.
*/
app.listen(app.get("port"), function() {
console.log("Node app is running at http://localhost:" + app.get("port"));
});
server.on('error', onError);
server.on('listening', onListening);
app.get("/hello", (req, res) => {
res.send({ greeting: "hello there!" });
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
// named pipe
if (isNaN(port))
return val;
// port number
if (port >= 0)
return port;
return false;
}
// Event listener for HTTP server "error" event.
function onError(error) {
if (error.syscall !== 'listen')
throw error;
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
// Event listener for HTTP server "listening" event.
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}