-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
55 lines (46 loc) · 1.59 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
const express = require("express");
const corsMiddleWare = require("cors");
// Auth middleware: our own code. Checks for the existence of a token in a header called `authentication`.
const authMiddleWare = require("./auth/middleware");
const authRouter = require("./routers/auth");
const { PORT } = require("./config/constants");
// Create an express app
const app = express();
/**
* Middlewares
*
* It is advisable to configure your middleware before configuring the routes
* If you configure routes before the middleware, these routes will not use them
*
*/
// CORS middleware: * Since our api is hosted on a different domain than our client
// we are are doing "Cross Origin Resource Sharing" (cors)
// Cross origin resource sharing is disabled by express by default
app.use(corsMiddleWare());
// express.json():be able to read request bodies of JSON requests a.k.a. body-parser
const bodyParserMiddleWare = express.json();
app.use(bodyParserMiddleWare);
/**
* Routes
*
* Define your routes and attach our routers here (now that middlewares are configured)
*/
app.use("/auth", authRouter);
// POST endpoint which requires a token for testing purposes, can be removed
app.post("/authorized_post_request", authMiddleWare, (req, res) => {
// accessing user that was added to req by the auth middleware
const user = req.user;
// don't send back the password hash
delete user.dataValues["password"];
res.json({
youPosted: {
...req.body,
},
userFoundWithToken: {
...user.dataValues,
},
});
});
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});