Estoy escribiendo API en node.js.
URL http://localhost:8000/api/request?connId=19&timeout=8000
cuando llamo a la API anterior desde Postman , se tarda 8 segundos en dar la respuesta. igual a lo esperado.
pero cuando llama a esto desde curl , da salida al instante.
curl http://localhost:8000/api/request?connId=19&timeout=8000El código es:
app.get('/api/request', ( req, res ) => { let timeoutID = null; // return by setTimeout let starttime = null; // time when request is begin const time = req.query.timeout; // time send by client const connId = req.query.connId; // id send by client // checking client data is valid or not if ( typeof(connId) !== undefined && typeof(time) !== undefined ) { starttime = new Date(); // get current time starttime = starttime.setMilliseconds(starttime.getMilliseconds()); // convert into millisecon // initiate setTimeout timeoutID = setTimeout(() => { // remove that element from global array which reqest has been complted removeByAttr(timeout, 'connId', connId); // res.send({ status: "ok"}); // send response }, time); // initiate setInterval const setInv = setInterval(() => { // check timeout [] conatin the current request const arrLength = timeout.length && timeout.filter(({ connId }) => req.query.connId === connId).length; if ( arrLength === 0 ) { res.send({ status: "ok"}); // send response clearInterval(setInv); // clear/destroy current Interval } }, 80); // insert the current request into global [] timeout timeout.push({ connId, time, timeoutID, starttime, 'endtime': +starttime + +time }); } });Como descubrí en su tutorial de código y curl , cuando obtiene un punto final con curl, su comando debería ser así:
curl "http://localhost:8000/api/request?connId=19&timeout=8000"
Sin doble coma invertida en la URL, console.log(req.query) dará como resultado { connId: '19' } que está causando el problema.