-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
75 lines (62 loc) · 1.96 KB
/
app.ts
File metadata and controls
75 lines (62 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
import express from 'express';
import cors from 'cors';
import envVars from './src/Config/envconfig';
import mongodb from './src/Mongodb/mongodb';
import routes from './src/Config/routes';
import cluster from 'cluster';
import os from 'os';
import helmet from 'helmet';
import morgan from 'morgan';
if (envVars.NODE_ENV === 'development') {
const app = express();
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
app.use(routes)
//Connecting to Mongo Database
const database = async () => {
const client = await mongodb().then().catch(err => { console.log(err) });
app.locals.db = client.db();
};
database().then(() => {
console.log('Database connected');
});
app.listen(envVars.PORT, () => {
//tslint:disable-next-line:no-console
console.log(`App is running on port ${envVars.PORT}`);
});
}
else {
const totalCPUs = os.cpus().length;
if (cluster.isPrimary) {
console.log(`Master PID is ${process.pid}`)
for (let index = 0; index < totalCPUs; index++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker with ${worker.process.pid} died`);
console.log(`Starting a new worker`);
cluster.fork();
})
}
else {
const app = express();
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(routes)
//Connecting to Mongo Database
const database = async () => {
const client = await mongodb().then().catch(err => { console.log(err) });
app.locals.db = client.db();
};
database().then(() => {
console.log('Database connected');
});
app.listen(envVars.PORT, () => {
//tslint:disable-next-line:no-console
console.log(`App is running on port ${envVars.PORT}`);
});
}
}