generated from Codaisseur/express-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
155 lines (143 loc) · 4.07 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require("dotenv").config();
const express = require("express");
const loggerMiddleWare = require("morgan");
const corsMiddleWare = require("cors");
const { PORT } = require("./config/constants");
const authRouter = require("./routers/auth");
const authMiddleWare = require("./auth/middleware");
const userRouter = require("./routers/user");
const projectRouter = require("./routers/projects");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
/**
* 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
*
*/
/**
* morgan:
*
* simple logging middleware so you can see
* what happened to your request
*
* example:
*
* METHOD PATH STATUS RESPONSE_TIME - Content-Length
*
* GET / 200 1.807 ms - 15
* POST /echo 200 10.251 ms - 26
* POST /puppies 404 1.027 ms - 147
*
* github: https://github.com/expressjs/morgan
*
*/
app.use(loggerMiddleWare("dev"));
/**
*
* express.json():
* be able to read request bodies of JSON requests
* a.k.a. body-parser
* Needed to be able to POST / PUT / PATCH
*
* docs: https://expressjs.com/en/api.html#express.json
*
*/
const bodyParserMiddleWare = express.json();
app.use(bodyParserMiddleWare);
/**
*
* 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
* for safety reasons (should everybody be able to use your api, I don't think so!)
*
* We are configuring cors to accept all incoming requests
* If you want to limit this, you can look into "white listing" only certain domains
*
* docs: https://expressjs.com/en/resources/middleware/cors.html
*
*/
app.use(corsMiddleWare());
/**
*
* delay middleware
*
* Since our api and client run on the same machine in development mode
* the request come in within milliseconds
* To simulate normal network traffic this simple middleware delays
* the incoming requests by 1500 second
* This allows you to practice with showing loading spinners in the client
*
* - it's only used when you use npm run dev to start your app
* - the delay time can be configured in the package.json
*/
if (process.env.DELAY) {
app.use((req, res, next) => {
setTimeout(() => next(), parseInt(process.env.DELAY));
});
}
/**
*
* authMiddleware:
*
* When a token is provided:
* decrypts a jsonwebtoken to find a userId
* queries the database to find the user with that add id
* adds it to the request object
* user can be accessed as req.user when handling a request
* req.user is a sequelize User model instance
*
* When no or an invalid token is provided:
* returns a 4xx reponse with an error message
*
* check: auth/middleware.js
*
* For fine grained control, import this middleware in your routers
* and use it for specific routes
*
* for a demo check the following endpoints
*
* POST /authorized_post_request
* GET /me
*
*/
/**
* Socket.io
*
*/
io.on("connection", (socket) => {
const admin = {
adminName: ".codeFolioBot",
adminMessage: "Welcome to the .codeFolio chatbox! Say hello to everyone!",
adminImage:
"https://static.vecteezy.com/system/resources/previews/000/692/252/original/retro-robot-vector-illustration.jpg",
};
const { adminName, adminMessage, adminImage } = admin;
socket.emit("message", {
name: adminName,
message: adminMessage,
image: adminImage,
});
console.log("connected!");
socket.on("message", ({ name, message, image }) => {
console.log({ name, message, image });
io.emit("message", { name, message, image });
});
});
/**
* Routes
*
* Define your routes here (now that middlewares are configured)
*/
app.use("/", authRouter);
app.use("/", userRouter);
app.use("/", projectRouter);
// Listen for connections on specified port (default is port 4000)
http.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});