-
Notifications
You must be signed in to change notification settings - Fork 195
/
worker.js
46 lines (33 loc) · 1.14 KB
/
worker.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
const express = require('express');
const connectDB = require('./src/db/mongoose');
const injectMiddleWares = require('./src/middleware');
const errorMiddleware = require('./src/middleware/error');
const authUser = require('./src/middleware/auth');
const routes = require('./src/routes');
const { loadDataInMemory, isDev, redirectFn } = require('./src/utils/util');
const { log } = require('./src/helpers/logger');
const { PORT = 8888 } = process.env;
const app = express();
setupApp();
async function setupApp() {
// use database to store logs and custom responses
await connectDB();
// load all data in memory
loadDataInMemory();
// set up all middleware
injectMiddleWares(app);
// set ejs as view engine
app.set('view engine', 'ejs');
// serving static files
app.use('/public', isDev ? express.static('public') : redirectFn);
// routes
app.use('/', routes);
// routes with authorization
app.use('/auth/', authUser, routes);
app.get('*', (req, res) => {
res.status(404).send();
});
// use custom middleware for errors
app.use(errorMiddleware);
app.listen(PORT, () => log(`[Worker] ${process.pid} started`));
}