-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (62 loc) · 1.82 KB
/
index.js
File metadata and controls
78 lines (62 loc) · 1.82 KB
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
const express = require('express');
const morgan = require('morgan');
const { format } = require('date-fns');
const cors = require('cors');
const authenticateJWT = require("./utilities/authenticateJWT");
const swaggerUi = require("swagger-ui-express")
const swaggerConfig = require("./swaggerConfig")
const checkJwt = require("./utilities/auth0");
const routes = require('./routes');
const { SERVER_PORT, CLIENT_ORIGIN } = require('./config');
const app = express();
morgan.token('date', () => format(new Date(), 'h:mm:ss a'));
app.use(
morgan(
'":method :url" :status :res[content-length] - :response-time ms [:date[America/Denver]]',
),
);
// app.use(
// cors({
// origin: false,
// }),
// );
app.use(
cors({
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE"], // Allow the methods your frontend will use
allowedHeaders: ["Content-Type", "Authorization"], // Specify headers if needed
})
);
app.use(express.json());
app.use(authenticateJWT)
app.use(routes);
app.use("/api/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerConfig))
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, _next) => {
if (err.status) {
const errBody = { ...err, message: err.message };
res.status(err.status).json(errBody);
} else {
console.error("There was an error")
res.status(500).json({ message: 'Internal Server Error' });
if (err.name !== 'FakeError') console.log(err);
}
});
function runServer(port = SERVER_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) {
runServer();
}
module.exports = app;