-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (60 loc) · 3.18 KB
/
index.js
File metadata and controls
73 lines (60 loc) · 3.18 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
const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
require('dotenv').config();
const cluster = require("cluster");
const { cpus } = require("os");
const { pid } = require("process");
const authRoutes = require('./Server/routes/auth_routes');
const userRoutes = require('./Server/routes/user_routes');
const employeeRoutes = require('./Server/routes/employee_routes');
const googleLoginRoutes = require('./Server/routes/googleoauth');
const resetPasswordRoutes = require('./Server/routes/reset_password');
const PORT = process.env.PORT || 8001;
const cors = require('cors');
const corsOptions = {
origin: ["https://hrconnectapi.onrender.com/", 'https://hrconnectapi.onrender.com/auth/signin', 'https://hrconnectapi.onrender.com/auth/signup', 'https://hrconnectapi.onrender.com/employee', "https://hrconnectapi.onrender.com/user/:id" , "https://hrconnectapi.onrender.com/google/status", "https://hrconnectapi.onrender.com/google/login", "http://localhost:3000", "*", "https://hr-connect.vercel.app/", "http://localhost:8001", "http://localhost:8001/users", "http://localhost:8001/employees", "http://localhost:8001/reset/forgotpassword", "http://localhost:8001/reset/status", "http://localhost:8001/reset/password/:token", "http://localhost:8001/auth", "http://localhost:8001/google", "http://localhost:10000", "https://hrconnectapi.onrender.com/reset/forgotpassword", "https://hrconnectapi.onrender.com/user/status", "https://hrconnectapi.onrender.com/reset/password/:token","https://hrconnectapi.onrender.com/user/files", cors()]
}
// Middlewares
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
mongoose.connect(`mongodb+srv://${process.env.APP_USER}:${process.env.APP_PASS}@webmobileapplication.jx4opz3.mongodb.net/${process.env.APP_COLLECTION}?retryWrites=true&w=majority`, {
useNewUrlParser: true,
}).then(() => {
console.log('Connected to MongoDB instance');
}).catch((err) => {
console.log(err.message);
});
if (cluster.isPrimary) {
console.log(`Primary ${pid} is running`);
// Fork workers.
for (let i = 0; i < cpus().length; i++) {
cluster.fork();
}
cluster.on("exit", (worker) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
app.get('/', (req, res) => {
res.json({message: "Welcome to my MongoDB API" });
});
//routes middleware
app.use('/auth', apiLimiter, authRoutes);
app.use('/users', apiLimiter, userRoutes);
app.use('/employees', apiLimiter, employeeRoutes);
app.use('/google', apiLimiter, googleLoginRoutes);
app.use('/reset', apiLimiter, resetPasswordRoutes);
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});
console.log(`Worker ${pid} started`);
}