-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (69 loc) · 1.96 KB
/
app.js
File metadata and controls
79 lines (69 loc) · 1.96 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
79
require("express-async-errors");
require("dotenv").config();
require("./modules/firebase/firebase_setup").initFirebase();
require("./modules/jobs/email_job");
const fileUpload = require("express-fileupload");
const { setupCloudConfig } = require("./modules/cloudinaryApis/connect");
const express = require("express");
const app = express();
const { connectDB } = require("./db/connect");
const { errorHandlerMiddleware } = require("./middleware/error-handler");
const rateLimit = require("express-rate-limit");
const posts = require("./routes/posts");
const users = require("./routes/users");
const os = require("os");
/*
TODO : FEATURES TO ADD :
2)secure apis using firebase user token (middleware)
6)add comments section to the application
(make sep collection for comments and use postid to identify comments for a certain post)
5)try to deploy on kubernetes
*/
//CPUs
const numCpus = os.cpus().length;
//port
const port = process.env.PORT || 4000;
//middleware
app.use(
fileUpload({
useTempFiles: true,
})
);
app.use(
rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests within above time interval
message: "Too many requests, please try again later",
})
);
app.use(express.json());
app.use("/api/v1/posts", posts);
app.use("/api/v1/users", users);
app.use(errorHandlerMiddleware); //cloudinary
setupCloudConfig();
module.exports.startServerWithUrl = (databaseUrl) => {
//database
const start = async () => {
try {
await connectDB(databaseUrl);
// if (cluster.isMaster) {
// for (let i = 0; i < numCpus; i++) {
// cluster.fork();
// }
// } else {
//------UNCOMMENT
// server = app.listen(
// port,
// console.log(
// `The server PID=${process.pid} is listening on port ${port}...`
// )
// );
//------UNCOMMENT
// }
} catch (error) {
console.log(error.toString());
}
};
start();
return app;
};