簡體   English   中英

如何使用 REQUEST 模塊在 nodejs 中調用多個請求?

[英]How to call multiple request in nodejs using REQUEST module?

我試圖讓它看起來更好,我正在使用請求模塊在同一台服務器上執行 http 請求,但使用不同的開發 ID 來檢索設備值。

我有它,如下所示,但是還有其他方法可以將它包含在一個電話中嗎?

這就是我所擁有的:

request("http://" + DEVLINK_IP.DEVLINK_IP + ":" + DEVLINK_port.DEVLINK_port + "/JSON?request=getstatus&ref=" + addons.Lightning[1].ref2, (error, response, body) => {
            let object = JSON.parse(body);});

request("http://" + DEVLINK_IP.DEVLINK_IP + ":" + DEVLINK_port.DEVLINK_port + "/JSON?request=getstatus&ref=" + addons.Lightning[0].ref1, (error, response, body2) => {
                let object2 = JSON.parse(body2);});

request("http://" + DEVLINK_IP.DEVLINK_IP + ":" + DEVLINK_port.DEVLINK_port + "/JSON?request=getstatus&ref=" + addons.Lightning[0].ref3, (error, response, body3) => {
                let object3 = JSON.parse(body3);});

request("http://" + DEVLINK_IP.DEVLINK_IP + ":" + DEVLINK_port.DEVLINK_port + "/JSON?request=getstatus&ref=" + addons.Lightning[0].ref4, (error, response, body4) => {
                let object3 = JSON.parse(body4);});

這是我的一些腳本,我用它來:

// Issue the request
        request("http://" + DEV_IP.DEV_IP + ":" + DEV_port.DEV_port + "/JSON?request=getstatus&ref=" + addons.Lightning[1].ref2, (error, response, body) => {
            let object = JSON.parse(body);
        request("http://" + DEV_IP.DEV_IP + ":" + DEV_port.DEV_port + "/JSON?request=getstatus&ref=" + addons.Lightning[0].ref1, (error, response, body2) => {
                let object2 = JSON.parse(body2);
                // If there has been an error, log it
                if (error) console.error(error);
                
                
                

                message.channel.send({
                    embed: {
                        color: 3447003,
                        author: {
                            name: "Malosa-Lightning add-on",
                            icon_url: "https://icons.iconarchive.com/icons/jaan-jaak/weather/256/thunder-lightning-storm-icon.png"
                        },
                        title: "Lightning Detection",
                        url: "http://google.com",
                        description: "Current live info",
                        fields: [{
                            name: "Detection:",
                            value: (object.Devices[0].status)
                        },
                        {
                            name: "Masked links",
                            value: "You can put [masked links](http://google.com) inside of rich embeds."
                        },
                        {
                            name: "Markdown",
                            value: "You can put all the *usual* **__Markdown__** inside of them."
                        }
                        ],
                        timestamp: new Date(),
                        footer: {
                            icon_url: client.user.avatarURL,
                            text: "© Example"
                        }
                    }
                });
            });
        });

我發現了。

我將其更改為 axios

使用時

const [response1, response2 ,response3] = await axios.all([
    axios.get("WEBSITE1"), 
    axios.get("WEBSITE2"), 
    axios.get("WEBSITE3"), 
]);

console.log (response1)
console.log (response2)
console.log (response3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM