generated from Borodutch/frontend-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cjs
55 lines (52 loc) · 1.44 KB
/
server.cjs
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
const path = require('path')
const express = require('express')
const compression = require('compression')
const morgan = require('morgan')
const cors = require('cors')
const cluster = require('cluster')
const { cpus } = require('os')
const ViteExpress = require('vite-express')
const numCPUs = cpus().length
process.env.NODE_ENV = 'production'
const CLIENT_BUILD_DIR = path.join(process.cwd(), 'dist')
if (cluster.isPrimary) {
console.log('🕺🕺🕺🕺🕺🕺🕺🕺🕺')
console.log('🕺 Starting server...')
console.log('🕺🕺🕺🕺🕺🕺🕺🕺🕺')
console.log(`Main ${process.pid} is running`)
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
cluster.on('exit', (worker) => {
console.log(`worker ${worker.process.pid} died`)
console.log('Forking a new process...')
cluster.fork()
})
} else {
const app = express()
app.use(compression())
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable('x-powered-by')
app.use(cors())
app.use(
'/',
express.static(CLIENT_BUILD_DIR, {
maxAge: '1h',
extensions: ['html'],
})
)
app.use(morgan('tiny'))
const port = process.env.PORT || 5173
ViteExpress.config({
transformer: (html) => {
return html
},
})
ViteExpress.listen(
app,
port,
console.log(
`Express server listening on port ${port} (worker id: ${cluster.worker.id})`
)
)
}