I would like to set up an API using express that can either multithread or multiprocess requests. For instance, below is an api that sleeps 5 seconds before sending a response. If I call it quickly 3 times, the first response will take 5 seconds, the second will take 10, and the third will take 15, indicating the requests were handled sequentially.
How do I architect an application that can handle the requests concurrently.
const express = require('express')
const app = express()
const port = 4000
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
app.get('/', (req, res) => {
sleep(5000).then(()=>{
res.send('Hello World!')
})
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Edit: request -> response