Merge pull request #41 from Legonzaur/main
Node Clusters and additional header for request compression
This commit is contained in:
@@ -139,7 +139,10 @@ router.get(
|
|||||||
',' +
|
',' +
|
||||||
Math.abs(new Date().getTime() - timeStart.getTime()) / 1000 + ',' + req.originalUrl
|
Math.abs(new Date().getTime() - timeStart.getTime()) / 1000 + ',' + req.originalUrl
|
||||||
)
|
)
|
||||||
res.status(200).send(data)
|
const dataString = JSON.stringify(data)
|
||||||
|
const buffer = Buffer.from(dataString)
|
||||||
|
const size = buffer.length
|
||||||
|
res.status(200).setHeader('X-File-Size', size).setHeader('Content-Type', 'application/json').send(buffer)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+27
-7
@@ -1,4 +1,6 @@
|
|||||||
import * as dotenv from 'dotenv'
|
import * as dotenv from 'dotenv'
|
||||||
|
import cluster from 'cluster'
|
||||||
|
import os from 'os'
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import cors from 'cors'
|
import cors from 'cors'
|
||||||
@@ -7,20 +9,37 @@ import { dbReady } from './db/db'
|
|||||||
import processAll from './process/process'
|
import processAll from './process/process'
|
||||||
import listenKills from './process/onKill'
|
import listenKills from './process/onKill'
|
||||||
|
|
||||||
const app = express()
|
const cCPUs = os.cpus().length;
|
||||||
|
|
||||||
const port = 3000
|
const port = 3000
|
||||||
|
|
||||||
app.use(express.json())
|
if (cluster.isPrimary) {
|
||||||
|
// Create a worker for each CPU
|
||||||
|
for (let i = 0; i < cCPUs; i++) {
|
||||||
|
cluster.fork();
|
||||||
|
}
|
||||||
|
cluster.on('online', function (worker) {
|
||||||
|
console.log('Worker ' + worker.process.pid + ' is online.');
|
||||||
|
});
|
||||||
|
cluster.on('exit', function (worker, code, signal) {
|
||||||
|
console.log('worker ' + worker.process.pid + ' died.');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
app.use(cors())
|
|
||||||
app.get('/', (req, res) => {
|
|
||||||
res.send('Tone client API online')
|
|
||||||
})
|
|
||||||
|
|
||||||
app.use('/', client)
|
|
||||||
|
|
||||||
export default
|
export default
|
||||||
new Promise(async (resolve, reject) => {
|
new Promise(async (resolve, reject) => {
|
||||||
|
if (!cluster.isPrimary) {
|
||||||
|
const app = express()
|
||||||
|
app.use(express.json())
|
||||||
|
app.use(cors({ exposedHeaders: ['X-File-Size', 'Content-Encoding'] }))
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.send('Tone client API online')
|
||||||
|
})
|
||||||
|
|
||||||
|
app.use('/', client)
|
||||||
|
|
||||||
await dbReady()
|
await dbReady()
|
||||||
await processAll()
|
await processAll()
|
||||||
await listenKills()
|
await listenKills()
|
||||||
@@ -28,4 +47,5 @@ export default
|
|||||||
console.log(`Tone client api listening on port ${port}`)
|
console.log(`Tone client api listening on port ${port}`)
|
||||||
resolve(listenServer)
|
resolve(listenServer)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user