-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (35 loc) · 1.11 KB
/
app.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
/** Express app for message.ly. */
const express = require("express");
const cors = require("cors");
const { authenticateJWT } = require("./middleware/auth");
const ExpressError = require("./expressError")
const app = express();
// allow both form-encoded and json body parsing
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// allow connections to all routes from any browser
app.use(cors());
// get auth token for all routes
app.use(authenticateJWT);
/** routes */
const authRoutes = require("./routes/auth");
const userRoutes = require("./routes/users");
const messageRoutes = require("./routes/messages");
app.use("/auth", authRoutes);
app.use("/users", userRoutes);
app.use("/messages", messageRoutes);
/** 404 handler */
app.use(function(req, res, next) {
const err = new ExpressError("Not Found", 404);
return next(err);
});
/** general error handler */
app.use(function(err, req, res, next) {
res.status(err.status || 500);
if (process.env.NODE_ENV != "test") console.error(err.stack);
return res.json({
error: err,
message: err.message
});
});
module.exports = app;