繁体   English   中英

在node.js中获取http状态代码

[英]Get http status code in node.js

因此,我正在尝试获取传递给功能的URL的http状态代码。 到目前为止,我已经使用了request模块,但是问题是请求获取了给定URL的所有内容,并且如果内容是大量数据,它的速度很慢,但是我只需要获取状态代码来制作if语句。

request(audioURL, function (error, response) {
    if (!error && response.statusCode == 200) {
        queue.push({
            title: videoTitle, 
            user: message.author.username, 
            mention: message.author.mention(), 
            url: audioURL
        });

        bot.reply(message, "\"" + videoTitle + "\" has been added to the queue.")
    }
    else {
        bot.reply(message, "Sorry, I couldn't get the audio track for that video. HTTP status code: " + response.statusCode);
    }
});

这就是我到目前为止所得到的。audioURL是一个基本字符串,带有指向媒体文件的链接

http.request可以将options Object作为其第一个参数。 使用这样的东西:

const options = {
    method: 'HEAD',
    host: 'example.com',
};

const req = http.request(options, (res) => {
    console.log(JSON.stringify(res.headers));
});

req.end();

问题是您需要将audioURL转换为其组件,以作为options参数传递。 我正在使用HEAD方法,该方法仅获取您请求的标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM