-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (78 loc) · 2.31 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
/*
* ElderPass - Check students in and out of Flex time
* Written by: Zack Sargent (add your name here)
*/
/**
* @module MainServer
*/
"use strict";
// dotenv gets the secrets we keep in the `.env` file.
const dotenv = require('dotenv');
// express is the web framework we are using to host files
// and serve the REST api
const express = require('express');
// create a server for express to listen on
const http = require('http');
// access local files
const path = require('path');
// these are all of the routes for the URL
const router = require('./routes/index');
const apiRouter = require('./routes/api');
// auth is provided by Auth0 (we get 7,000 signups for free)
const { auth } = require('express-openid-connect');
const { morgan, logger, morganMode, rfsStream } = require("./src/loggers");
process.env.TZ = "America/New_York"; // the time zone this server should be operating in
dotenv.load();
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(morgan(morganMode, {
stream: rfsStream
}));
app.use(morgan('dev'));
app.use("/public", express.static(path.join(__dirname, 'public')));
app.use(express.json());
const config = {
authRequired: false,
auth0Logout: true
};
const port = process.env.PORT || 3000;
if (!config.baseURL && !process.env.BASE_URL && process.env.PORT && process.env.NODE_ENV !== 'production') {
config.baseURL = `http://localhost:${port}`;
}
app.use(auth(config));
// Middleware to make the `user` object available for all views
app.use(function (req, res, next) {
res.locals.user = req.oidc.user;
next();
});
/**
* @name /
* @description route to the [router]{@link module:Router}
*/
app.use('/', router);
/**
* @name /api
* @description route to the [API]{@link module:API}
*/
app.use('/api', apiRouter);
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handlers
app.use(function (err, req, res, next) {
res.status(err.status || 500);
logger.error(JSON.stringify(err));
logger.trace(req);
res.render('error', {
message: err.message,
error: process.env.NODE_ENV !== 'production' ? err : {}
});
});
http.createServer(app)
.listen(port, () => {
logger.info(`Listening on ${config.baseURL}`);
});