-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (40 loc) · 1.51 KB
/
Copy pathindex.js
File metadata and controls
43 lines (40 loc) · 1.51 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
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const morgan = require('morgan')
const moment = require('moment-timezone')
const config = require('./config/config')
const routes = require('./route/routes.js')
const cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
console.log('restarting app..')
cluster.fork();
});
}
if (cluster.isWorker) {
process.env.BASEDIR = __dirname
app.use((req, res, next)=>{
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Credentials', 'true')
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DEconstE')
res.header('Access-Control-Expose-Headers', 'Content-Length')
res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range')
if (req.method === 'OPTIONS') {
return res.send(200)
} else {
return next()
}
})
morgan.token('date', (req, res, tz) => {
return moment().tz(tz).format('DD MMMM YYYY hh:mm:ss');
})
morgan.format('myformat', ':remote-addr [:date[Asia/Jakarta]] ":method :url :http-version" :status :res[content-length] - :response-time ms ":user-agent"');
app.use(bodyParser.json())
app.use(morgan('myformat'))
routes(app)
app.listen(config.port, ()=>{
console.log('app listening at port %s', config.port)
})
}