generated from codepath/site-capstone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
48 lines (42 loc) · 1.47 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
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const security = require("./middleware/security");
const { PORT } = require("./config");
const authRoutes = require("./routes/auth");
const taskRoutes = require("./routes/tasks");
const { NotFoundError } = require("./utils/errors");
const timerSessionRoutes = require("./routes/TimerSession");
const app = express();
// enable cross-origin resource sharing for all origins for all requests
app.use(cors());
// log requests info
app.use(morgan("tiny"));
// parse incoming requests with JSON payloads
app.use(express.json());
// for every request, check if a token exists
// in the authorization header
// if it does attach the decoded user to res.locals
app.use(security.extractUserFromJwt);
// set routes!
app.use("/auth", authRoutes);
app.use("/task", taskRoutes);
app.use("/TimerSession", timerSessionRoutes);
/** Handle 404 errors -- this matches everything
* if the endpoint that the user sends a request to does not match any of our endpoints in our app
* this middleware will be called
*/
app.use((req, res, next) => {
return next(new NotFoundError());
});
/** Generic error handler; anything unhandled goes here. */
app.use((err, req, res, next) => {
const status = err.status || 500;
const message = err.message;
return res.status(status).json({
error: { message, status },
});
});
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
});