I am trying to write a Node.js application which accepts incoming requests from client and then make a call to some web service on the remote server to retrieve data.
const express = require('express')
const request = require('request')
const moment = require('moment')
const app = express()
app.get('/', (req, res) => {
request('http://localhost/sleep.php', (error, response, body) => {
res.send('get data at ' + moment().format())
})
})
app.listen(3000)
The remote service is written in PHP:
<?php
sleep(10);
echo 'Return something!';
The problem is that if the new request comes in then Node.js is blocked until it has finished the last callback. How to fix this? Any ideas?
Update:
I actually make two requests at the same time via Firefox and the second request spent almost 20 seconds.