2017-04-15 5 views
2

私はnode.jsにAPIを書いています。私はポストマンからAPIの上に呼び出すときカールと郵便配達は異なる応答を与えていますか?

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

は、それが応答を与えるために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 
     }); 
    } 
}); 
+0

'const'キーワードを' let'に変更してみてください。 – Chinwobble

+0

すべてのconstをletに変更すると動作しない –

+0

Postmanは特別なクロスドメイン権限を取得します。セキュリティが低下している可能性がありますか? –

答えて

3

私はあなたのコードとcurl tutorial thatから見つかったとして、あなたはカールとエンドポイントを取得するときに、あなたのコマンドは次のようにする必要があります:

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

ダブルなければconsole.log(req.query)にある逆括弧は、問題を引き起こしている{ connId: '19' }になります。

関連する問題