节点js中的重试不起作用

时间:2017-12-24 19:05:11

标签: node.js http express http-status-code-504 got

我正在运行http服务器,它始终返回504:

const express = require('express')
const app = express()

app.get('/', (req, res) => {
    console.log('I AM HERE');
    res.status(504).send('Not found!!');
})

app.listen(3000, () => console.log('Listening on port 3000!'))

之后,我正在做请求,使用got进行5次重试:

const got = require('got');

(async () => {
    try {
        const response = await got('http://localhost:3000/', {retries: 5});
        console.log(response.body);
    } catch (error) {
        console.log(error.response.body);
    }
})();

我期待,铭文I AM HERE将被记录5次,但它只记录一次。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

该参数为重试不重试,这里是工作示例

try {
    const response = await got('https://example.com', {retry: 2});
    return response.body
 } catch (error) {
    console.log(error.response.body);   
    return error
 }