Javascript 在node.js中获取http状态代码
因此,我试图获取传递给函数的URL的http状态代码。到目前为止,我已经使用了request模块,但问题是request会获取给定URL的所有内容,如果内容是大量数据,那么速度会很慢,但我只需要获取状态代码来生成if语句Javascript 在node.js中获取http状态代码,javascript,node.js,http,http-status-codes,Javascript,Node.js,Http,Http Status Codes,因此,我试图获取传递给函数的URL的http状态代码。到目前为止,我已经使用了request模块,但问题是request会获取给定URL的所有内容,如果内容是大量数据,那么速度会很慢,但我只需要获取状态代码来生成if语句 request(audioURL, function (error, response) { if (!error && response.statusCode == 200) { queue.push({ tit
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。请求可以将选项
对象作为其第一个参数。使用类似以下内容:
const options = {
method: 'HEAD',
host: 'example.com',
};
const req = http.request(options, (res) => {
console.log(JSON.stringify(res.headers));
});
req.end();
问题是您需要将audioURL
转换为其组件,以作为选项的参数传递。我使用的是HEAD
方法,它只获取请求的头。http。请求可以将选项
对象作为其第一个参数。使用类似以下内容:
const options = {
method: 'HEAD',
host: 'example.com',
};
const req = http.request(options, (res) => {
console.log(JSON.stringify(res.headers));
});
req.end();
问题是您需要将audioURL
转换为其组件,以作为选项的参数传递。我使用的是HEAD
方法,它只获取请求的标题