-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (77 loc) · 2.74 KB
/
index.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
'use strict';
/* ===== required library's/files ===== */
const express = require('express'); // node.js web application framework
const cors = require('cors'); // allows the client and server to speak to each other
const morgan = require('morgan'); // HTTP request logger middleware for node.js
const dotenv = require('dotenv').config(); // allows the .env variables to be read by the config file
const passport = require('passport'); // authentication middleware
const { PORT, CLIENT_ORIGIN } = require('./config');
const { dbConnect } = require('./db-mongoose');
const localStrategy = require('./passport/localStrategy'); // strategy for authenticating with a un and pw.
const jwtStrategy = require('./passport/jwtStrategy'); // strategy for authenticating with jwt's.
const registerRouter = require('./routes/registerRouter');
const teamRouter = require('./routes/teamRouter');
const proxyRouter = require('./routes/proxyRouter');
const loginRouter = require('./routes/loginRouter');
const leagueRouter = require('./routes/leagueRouter');
/* ===== use express ===== */
const app = express();
/* ===== how to use morgan based upon the current environment ===== */
app.use(
morgan(process.env.NODE_ENV === 'production' ? 'common' : 'dev', {
skip: (req, res) => process.env.NODE_ENV === 'test'
})
);
/* ===== allow the clients domain to access the server info ===== */
app.use(
cors({
origin: CLIENT_ORIGIN
})
);
/* ===== built in express middleware for decoding the request body ===== */
app.use(express.json());
/* ===== middleware for authenticating with passport ===== */
passport.use(localStrategy);
passport.use(jwtStrategy);
/* ===== endpoints ===== */
app.use('/register', registerRouter);
app.use('/login', loginRouter);
app.use('/team', teamRouter);
app.use('/api', proxyRouter);
app.use('/league', leagueRouter);
/* ===== error handling ===== */
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: app.get('env') === 'development' ? err : {}
});
});
/* ===== code that "nodemon" runs ===== */
function runServer(port = PORT) {
const server = app
.listen(port, () => {
console.info(`App listening on port ${server.address().port}`);
})
.on('error', err => {
console.error('Express failed to start');
console.error(err);
});
}
if (require.main === module) {
dbConnect();
runServer();
}
module.exports = {app};
/**
Resources:
- express: http://expressjs.com/en/api.html
- morgan: https://github.com/expressjs/morgan
- passport: http://www.passportjs.org/
- passport.authenticate: https://github.com/themikenicholson/passport-jwt
*/