【问题标题】:Curl and Postman giving different response?卷曲和邮递员给出不同的回应?
【发布时间】:2017-09-11 08:43:02
【问题描述】:

我正在 node.js 中编写 API。

网址http://localhost:8000/api/request?connId=19&timeout=8000

当我从 Postman 调用上述 API 时,需要 8 秒才能给出响应。和预期的一样。

但是当从 curl 调用它时,它会立即给出输出。

curl http://localhost:8000/api/request?connId=19&timeout=8000

代码是:

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
        });
    } 
});

【问题讨论】:

  • 尝试将 const 关键字更改为 let ?
  • 将所有 const 更改为 let 不起作用
  • Postman 获得特殊的跨域权限。安全性可能会减慢速度吗?
  • @KolobCanyon 但我编写的 API 必须在给定时间后发送响应。目前还没有安全应用

标签: node.js linux express curl postman


【解决方案1】:

正如我从您的代码和curl tutorial that 中发现的那样,当您使用 curl 获取端点时,您的命令应该是这样的:

curl "http://localhost:8000/api/request?connId=19&timeout=8000"

如果 url 中没有双引号,console.log(req.query) 将导致导致问题的{ connId: '19' }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2019-07-07
    • 2016-06-17
    • 1970-01-01
    • 2019-03-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多