Javascript node.js 检查远程 URL 是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26007187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js check if a remote URL exists
提问by metalaureate
How do I check to see if a URL exists without pulling it down? I use the following code, but it downloads the whole file. I just need to check that it exists.
如何在不下拉的情况下检查 URL 是否存在?我使用以下代码,但它会下载整个文件。我只需要检查它是否存在。
app.get('/api/v1/urlCheck/', function (req,res) {
var url=req.query['url'];
var request = require('request');
request.get(url, {timeout: 30000, json:false}, function (error, result) {
res.send(result.body);
});
});
Appreciate any help!
感谢任何帮助!
回答by danwarfel
Try this:
尝试这个:
var http = require('http'),
options = {method: 'HEAD', host: 'stackoverflow.com', port: 80, path: '/'},
req = http.request(options, function(r) {
console.log(JSON.stringify(r.headers));
});
req.end();
回答by Nico
Thanks! Here it is, encapsulated in a function (updated on 5/30/17 with the requireoutside):
谢谢!在这里,封装在一个函数中(于 2017 年 5 月 30 日更新,外面有require):
var http = require('http'),
url = require('url');
exports.checkUrlExists = function (Url, callback) {
var options = {
method: 'HEAD',
host: url.parse(Url).host,
port: 80,
path: url.parse(Url).pathname
};
var req = http.request(options, function (r) {
callback( r.statusCode== 200);});
req.end();
}
It's very quick (I get about 50 ms, but it will depend on your connection and the server speed). Note that it's also quite basic, i.e. it won't handle redirects very well...
它非常快(我大约需要 50 毫秒,但这取决于您的连接和服务器速度)。请注意,它也很基本,即它不能很好地处理重定向...
回答by Rakesh Soni
Simply use url-existsnpm package to test if url exists or not
只需使用url-existsnpm 包来测试 url 是否存在
var urlExists = require('url-exists');
urlExists('https://www.google.com', function(err, exists) {
console.log(exists); // true
});
urlExists('https://www.fakeurl.notreal', function(err, exists) {
console.log(exists); // false
});
回答by Richie Bendall
2020 update
2020年更新
requesthas now been deprecated which has brought down url-existswith it. Use url-existinstead.
request现在已被弃用,这导致了url-exists它的失败。使用url-exist来代替。
const urlExist = require("url-exist");
(async () => {
const exists = await urlExist("https://google.com");
// Handle result
console.log(exists)
})();
If you (for some reason) need to use it synchronously, you can use url-exist-sync.
如果您(出于某种原因)需要同步使用它,则可以使用url-exist-sync.
2019 update
2019年更新
Since 2017, requestand callback-style functions (from url-exists) have fallen out of use.
自 2017 年以来,request回调式函数 (from url-exists) 已不再使用。
However, there is a fix. Swap url-existsfor url-exist.
但是,有一个修复方法。交换url-exists的url-exist。
So instead of using:
所以不要使用:
const urlExists = require("url-exists")
urlExists("https://google.com", (_, exists) => {
// Handle result
console.log(exists)
})
Use this:
用这个:
const urlExist = require("url-exist");
(async () => {
const exists = await urlExist("https://google.com");
// Handle result
console.log(exists)
})();
Original answer (2017)
原始答案 (2017)
If you have access to the requestpackage, you can try this:
如果您有权访问该request软件包,则可以尝试以下操作:
const request = require("request")
const urlExists = url => new Promise((resolve, reject) => request.head(url).on("response", res => resolve(res.statusCode.toString()[0] === "2")))
urlExists("https://google.com").then(exists => console.log(exists)) // true
Most of this logic is already provided by url-exists.
大部分逻辑已经由url-exists.
回答by Deejers
Take a look at the url-existsnpm package https://www.npmjs.com/package/url-exists
看看url-existsnpm 包https://www.npmjs.com/package/url-exists
Setting up:
配置:
$ npm install url-exists
Useage:
用途:
const urlExists = require('url-exists');
urlExists('https://www.google.com', function(err, exists) {
console.log(exists); // true
});
urlExists('https://www.fakeurl.notreal', function(err, exists) {
console.log(exists); // false
});
You can also promisify it to take advantage of awaitand async:
您还可以承诺它以利用await和async:
const util = require('util');
const urlExists = util.promisify(require('url-exists'));
let isExists = await urlExists('https://www.google.com'); // true
isExists = await urlExists('https://www.fakeurl.notreal'); // false
Happy coding!
快乐编码!
回答by Podolsky
requireinto functions is wrong way in Node.
Followed ES6 method supports all correct http statuses and of course retrieve error if you have a bad 'host' like fff.kkk
require在 Node.js 中进入函数是错误的。遵循的 ES6 方法支持所有正确的 http 状态,当然,如果您有像 fff.kkk 这样的坏“主机”,则检索错误
checkUrlExists(host,cb) {
http.request({method:'HEAD',host,port:80,path: '/'}, (r) => {
cb(null, r.statusCode >= 200 && r.statusCode < 400 );
}).on('error', cb).end();
}
回答by mjlescano
Using the other responses as reference, here's a promisified version which also works with httpsuris (for node 6+):
使用其他响应作为参考,这里有一个 promisified 版本,它也适用于httpsuris(对于 node 6+):
const http = require('http');
const https = require('https');
const url = require('url');
const request = (opts = {}, cb) => {
const requester = opts.protocol === 'https:' ? https : http;
return requester.request(opts, cb);
};
module.exports = target => new Promise((resolve, reject) => {
let uri;
try {
uri = url.parse(target);
} catch (err) {
reject(new Error(`Invalid url ${target}`));
}
const options = {
method: 'HEAD',
host: uri.host,
protocol: uri.protocol,
port: uri.port,
path: uri.path,
timeout: 5 * 1000,
};
const req = request(options, (res) => {
const { statusCode } = res;
if (statusCode >= 200 && statusCode < 300) {
resolve(target);
} else {
reject(new Error(`Url ${target} not found.`));
}
});
req.on('error', reject);
req.end();
});
It can be used like this:
它可以像这样使用:
const urlExists = require('./url-exists')
urlExists('https://www.google.com')
.then(() => {
console.log('Google exists!');
})
.catch(() => {
console.error('Invalid url :(');
});
回答by BananaAcid
my awaitable async ES6 solution, doing a HEAD request:
我等待的异步 ES6 解决方案,执行 HEAD 请求:
// options for the http request
let options = {
host: 'google.de',
//port: 80, optional
//path: '/' optional
}
const http = require('http');
// creating a promise (all promises a can be awaited)
let isOk = await new Promise(resolve => {
// trigger the request ('HEAD' or 'GET' - you should check if you get the expected result for a HEAD request first (curl))
// then trigger the callback
http.request({method:'HEAD', host:options.host, port:options.port, path: options.path}, result =>
resolve(result.statusCode >= 200 && result.statusCode < 400)
).on('error', resolve).end();
});
// check if the result was NOT ok
if (!isOk)
console.error('could not get: ' + options.host);
else
console.info('url exists: ' + options.host);
回答by Darren G
If you're using axios, you can fetch the head like:
如果你使用axios,你可以像这样获取头部:
const checkUrl = async (url) => {
try {
await axios.head(fullUrl);
return true;
} catch (error) {
if (error.response.status >= 400) {
return false;
}
}
}
You may want to customise the status coderange for your requirements e.g. 401 (Unauthorized) could still mean a URL exists but you don't have access.
您可能希望根据您的要求自定义状态代码范围,例如 401(未经授权)仍可能意味着 URL 存在但您无权访问。
回答by José Antonio Postigo
I see in your code that you are already using the requestlibrary, so just:
我在您的代码中看到您已经在使用该request库,所以只需:
const request = require('request');
request.head('http://...', (error, res) => {
const exists = !error && res.statusCode === 200;
});

