This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
128 lines (110 loc) · 4.37 KB
/
app.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
const bodyParser = require('body-parser');
const cluster = require('cluster');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv');
const express = require('express');
const favicon = require('serve-favicon');
const http = require('http');
const i18n = require('i18n');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
const path = require('path');
const session = require('express-session');
dotenv.config({ path: path.join(__dirname, '.env') });
const numCPUs = process.env.WEB_CONCURRENCY || require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++)
cluster.fork();
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
const app = express();
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/node101';
const MAX_SERVER_UPLOAD_LIMIT = 52428800;
const MAX_SERVER_PARAMETER_LIMIT = 50000;
const QUERY_LIMIT = 20;
const adminRouteController = require('./routes/adminRoute');
const apiRouteController = require('./routes/apiRoute');
const authRouteController = require('./routes/authRoute');
const blogRouteController = require('./routes/blogRoute');
const bookRouteController = require('./routes/bookRoute');
const eventRouteController = require('./routes/eventRoute');
const guideRouteController = require('./routes/guideRoute');
const indexRouteController = require('./routes/indexRoute');
const memberRouteController = require('./routes/memberRoute');
const projectRouteController = require('./routes/projectRoute');
const stakeRouteController = require('./routes/stakeRoute');
const tagRouteController = require('./routes/tagRoute');
const venueRouteController = require('./routes/venueRoute');
const wizardRouteController = require('./routes/wizardRoute');
const writerRouteController = require('./routes/writerRoute');
const writingRouteController = require('./routes/writingRoute');
const fromDateToHTMLInputString = require('./utils/fromDateToHTMLInputString');
// const updateData = require('./utils/updateData');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
mongoose.set('strictQuery', false);
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
i18n.configure({
locales:['tr', 'en'],
directory: __dirname + '/translations',
queryParameter: 'lang',
defaultLocale: 'tr'
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.json({ limit: MAX_SERVER_UPLOAD_LIMIT }));
app.use(bodyParser.urlencoded({
extended: true,
limit: MAX_SERVER_UPLOAD_LIMIT,
parameter: MAX_SERVER_PARAMETER_LIMIT
}));
app.use(i18n.init);
const sessionOptions = session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: MONGODB_URI
})
});
app.use(cookieParser());
app.use(sessionOptions);
app.use((req, res, next) => {
if (!req.query || typeof req.query != 'object')
req.query = {};
if (!req.body || typeof req.body != 'object')
req.body = {};
res.locals.QUERY_LIMIT = QUERY_LIMIT;
res.locals.fromDateToHTMLInputString = fromDateToHTMLInputString;
req.query.limit = QUERY_LIMIT;
next();
});
app.use('/', indexRouteController);
app.use('/admin', adminRouteController);
app.use('/api', apiRouteController);
app.use('/auth', authRouteController);
app.use('/blog', blogRouteController);
app.use('/book', bookRouteController);
app.use('/event', eventRouteController);
app.use('/guide', guideRouteController);
app.use('/member', memberRouteController);
app.use('/project', projectRouteController);
app.use('/stake', stakeRouteController);
app.use('/tag', tagRouteController);
app.use('/venue', venueRouteController);
app.use('/wizard', wizardRouteController);
app.use('/writer', writerRouteController);
app.use('/writing', writingRouteController);
server.listen(PORT, () => {
console.log(`Server is on port ${PORT} as Worker ${cluster.worker.id} running @ process ${cluster.worker.process.pid}`);
});
}