NodeJS url驗(yàn)證(url-valid)的使用方法
更新時(shí)間:2013年11月18日 09:10:43 作者:
本文主要介紹了NodeJS url驗(yàn)證(url-valid)模塊的使用方法,最后提供了實(shí)例代碼供大家參考
Javascript做url檢驗(yàn),通常是使用正則表達(dá)式來(lái)判定,其格式是否正確,例如:
/^https?:\/\//.test(url);
當(dāng)然還有更好的檢測(cè)方法比如基于RFC 3986, RFC 3966, RFC 4694, RFC 4759, RFC 4904等標(biāo)準(zhǔn)的進(jìn)行驗(yàn)證的valid-url庫(kù)。
不過(guò)個(gè)根據(jù)格式進(jìn)行驗(yàn)證當(dāng)然不能確定該url是否存在啦,所以就有了url-valid,我們基于HTTP請(qǐng)求進(jìn)行驗(yàn)證。
接口設(shè)計(jì)
實(shí)際上我們只需要一個(gè)函數(shù)傳入一個(gè)url地址,并回調(diào)返回該鏈接是否可用。
但請(qǐng)求容易產(chǎn)生未知錯(cuò)誤,所以我們?cè)诨卣{(diào)函數(shù)傳入一個(gè)error參數(shù),如果不為空,則有錯(cuò)誤產(chǎn)生。
我們可能還希望能夠得到網(wǎng)頁(yè)的相關(guān)數(shù)據(jù),未來(lái)用在頁(yè)面的信息提取上。
盡可能鏈?zhǔn)讲僮靼伞?BR>所以最后使用上大概是這樣的:
valid(url)
.on('check', function (err, status) {
if (err) throw err;
status ?
console.log('url是可用的') :
console.log('url是不可用的');
})
.on('data', function (err, data) {
console.log(data);
})
.on('end', function (err, data) {
console.log('請(qǐng)求結(jié)束');
})
HTTP GET 還是 HTTP HEAD
本來(lái)我們想利用HTTP HEAD請(qǐng)求來(lái)實(shí)現(xiàn)的,因?yàn)镠EAD請(qǐng)求只會(huì)返回頭信息,這可以減少請(qǐng)求時(shí)間,但是HEAD請(qǐng)求,不一定所有鏈接都會(huì)支持。
所以最后我們使用HTTP GET方式,在得到正確的statusCode后立刻abort掉請(qǐng)求。
處理301-303
因?yàn)?01到303都是重定向狀態(tài)所以,我們需要繼續(xù)檢查對(duì)應(yīng)Location是否依然存在。
利用process.nextTick異步執(zhí)行
為了在注冊(cè)監(jiān)聽(tīng)后,再執(zhí)行代碼,我們使用process.nextTick來(lái)一步操作。
實(shí)現(xiàn)
/*!
* valid
* MIT Licensed
*/
module.exports = (function () {
'use strict';
var http = require('http')
, https = require('https')
, EventEmitter = require('events').EventEmitter
, URL = require('url')
, urlReg = /^(https?):\/\//;
/**
* Valid
* @class
*/
function Valid(url, callback) {
var that = this;
this.url = url;
this.emitter = new EventEmitter();
process.nextTick(function () {
that.get(url);
});
this.fetch = false;
callback && this.emitter.on('check', callback);
}
Valid.prototype = {
constructor: Valid,
/**
* get
* @param {String} url
*/
get: function (url) {
var match = url.match(urlReg)
, that = this;
if (match) {
var httpLib = (match[1].toLowerCase() === 'http') ? http : https
, opts = URL.parse(url)
, req;
opts.agent = false;
opts.method = 'GET';
req = httpLib.request(opts, function (res) {
var statusCode = res.statusCode;
if (statusCode === 200) {
that.emitter.emit('check', null, true);
that.fetch ?
(res.on('data', function (data) {
that.emitter.emit('data', null, data);
}) && res.on('end', function () {
that.emitter.emit('end');
})) :
(req.abort() || that.emitter.emit('end'));
} else if (300 < statusCode && statusCode < 304) {
req.abort();
var emitter = that.emitter
, valid = one(URL.resolve(url, res.headers.location), function (err, valid) {
emitter.emit('check', err, valid);
});
that.fetch && valid.on('data', function (err, data) {
emitter.emit('data', err, data);
});
valid.on('error', function (err) {
that.emitter.emit('error', err);
});
valid.on('end', function () {
that.emitter.emit('end');
});
} else {
that.emitter.emit('check', null, false);
}
res.on('error', function (err) {
req.abort();
that.emitter.emit('data', err);
});
});
req.on('error', function (err) {
req.abort();
return that.emitter.emit('check', null, false);
});
req.end();
} else {
return that.emitter.emit('check', null, false);
}
},
/**
* on
* @param {Stirng} event
* @param {Function} callback
*/
on: function (event, callback) {
(event === 'data') && (this.fetch = true);
this.emitter.on(event, callback);
return this;
},
/**
* destroy
*/
destroy: function () {
this.emitter.removeAllListeners();
this.url = undefined;
this.emitter = null;
this.fetch = undefined;
},
/**
* removeAllListeners
* @param
*/
removeAllListeners: function (event) {
event ?
this.emitter.removeAllListeners(event) :
this.emitter.removeAllListeners();
return this;
},
/**
* listeners
* @param
*/
listeners: function (event) {
if (event) {
return this.emitter.listeners(event);
} else {
var res = []
, that = this
, _push = Array.prototype.push;
Object.keys(this.emitter._events).forEach(function (key) {
_push.apply(res, that.emitter.listeners(key));
});
return res;
}
}
}
/**
* one
* @param {String} url
* @param {Function} callback
* @return {Valid}
*/
function one(url, callback) {
return (new Valid(url, callback));
}
one.one = one;
return one;
})();
復(fù)制代碼 代碼如下:
/^https?:\/\//.test(url);
當(dāng)然還有更好的檢測(cè)方法比如基于RFC 3986, RFC 3966, RFC 4694, RFC 4759, RFC 4904等標(biāo)準(zhǔn)的進(jìn)行驗(yàn)證的valid-url庫(kù)。
不過(guò)個(gè)根據(jù)格式進(jìn)行驗(yàn)證當(dāng)然不能確定該url是否存在啦,所以就有了url-valid,我們基于HTTP請(qǐng)求進(jìn)行驗(yàn)證。
接口設(shè)計(jì)
實(shí)際上我們只需要一個(gè)函數(shù)傳入一個(gè)url地址,并回調(diào)返回該鏈接是否可用。
但請(qǐng)求容易產(chǎn)生未知錯(cuò)誤,所以我們?cè)诨卣{(diào)函數(shù)傳入一個(gè)error參數(shù),如果不為空,則有錯(cuò)誤產(chǎn)生。
我們可能還希望能夠得到網(wǎng)頁(yè)的相關(guān)數(shù)據(jù),未來(lái)用在頁(yè)面的信息提取上。
盡可能鏈?zhǔn)讲僮靼伞?BR>所以最后使用上大概是這樣的:
復(fù)制代碼 代碼如下:
valid(url)
.on('check', function (err, status) {
if (err) throw err;
status ?
console.log('url是可用的') :
console.log('url是不可用的');
})
.on('data', function (err, data) {
console.log(data);
})
.on('end', function (err, data) {
console.log('請(qǐng)求結(jié)束');
})
HTTP GET 還是 HTTP HEAD
本來(lái)我們想利用HTTP HEAD請(qǐng)求來(lái)實(shí)現(xiàn)的,因?yàn)镠EAD請(qǐng)求只會(huì)返回頭信息,這可以減少請(qǐng)求時(shí)間,但是HEAD請(qǐng)求,不一定所有鏈接都會(huì)支持。
所以最后我們使用HTTP GET方式,在得到正確的statusCode后立刻abort掉請(qǐng)求。
處理301-303
因?yàn)?01到303都是重定向狀態(tài)所以,我們需要繼續(xù)檢查對(duì)應(yīng)Location是否依然存在。
利用process.nextTick異步執(zhí)行
為了在注冊(cè)監(jiān)聽(tīng)后,再執(zhí)行代碼,我們使用process.nextTick來(lái)一步操作。
實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
/*!
* valid
* MIT Licensed
*/
module.exports = (function () {
'use strict';
var http = require('http')
, https = require('https')
, EventEmitter = require('events').EventEmitter
, URL = require('url')
, urlReg = /^(https?):\/\//;
/**
* Valid
* @class
*/
function Valid(url, callback) {
var that = this;
this.url = url;
this.emitter = new EventEmitter();
process.nextTick(function () {
that.get(url);
});
this.fetch = false;
callback && this.emitter.on('check', callback);
}
Valid.prototype = {
constructor: Valid,
/**
* get
* @param {String} url
*/
get: function (url) {
var match = url.match(urlReg)
, that = this;
if (match) {
var httpLib = (match[1].toLowerCase() === 'http') ? http : https
, opts = URL.parse(url)
, req;
opts.agent = false;
opts.method = 'GET';
req = httpLib.request(opts, function (res) {
var statusCode = res.statusCode;
if (statusCode === 200) {
that.emitter.emit('check', null, true);
that.fetch ?
(res.on('data', function (data) {
that.emitter.emit('data', null, data);
}) && res.on('end', function () {
that.emitter.emit('end');
})) :
(req.abort() || that.emitter.emit('end'));
} else if (300 < statusCode && statusCode < 304) {
req.abort();
var emitter = that.emitter
, valid = one(URL.resolve(url, res.headers.location), function (err, valid) {
emitter.emit('check', err, valid);
});
that.fetch && valid.on('data', function (err, data) {
emitter.emit('data', err, data);
});
valid.on('error', function (err) {
that.emitter.emit('error', err);
});
valid.on('end', function () {
that.emitter.emit('end');
});
} else {
that.emitter.emit('check', null, false);
}
res.on('error', function (err) {
req.abort();
that.emitter.emit('data', err);
});
});
req.on('error', function (err) {
req.abort();
return that.emitter.emit('check', null, false);
});
req.end();
} else {
return that.emitter.emit('check', null, false);
}
},
/**
* on
* @param {Stirng} event
* @param {Function} callback
*/
on: function (event, callback) {
(event === 'data') && (this.fetch = true);
this.emitter.on(event, callback);
return this;
},
/**
* destroy
*/
destroy: function () {
this.emitter.removeAllListeners();
this.url = undefined;
this.emitter = null;
this.fetch = undefined;
},
/**
* removeAllListeners
* @param
*/
removeAllListeners: function (event) {
event ?
this.emitter.removeAllListeners(event) :
this.emitter.removeAllListeners();
return this;
},
/**
* listeners
* @param
*/
listeners: function (event) {
if (event) {
return this.emitter.listeners(event);
} else {
var res = []
, that = this
, _push = Array.prototype.push;
Object.keys(this.emitter._events).forEach(function (key) {
_push.apply(res, that.emitter.listeners(key));
});
return res;
}
}
}
/**
* one
* @param {String} url
* @param {Function} callback
* @return {Valid}
*/
function one(url, callback) {
return (new Valid(url, callback));
}
one.one = one;
return one;
})();
您可能感興趣的文章:
- node.js中的url.parse方法使用說(shuō)明
- nodejs實(shí)現(xiàn)獲取當(dāng)前url地址及url各種參數(shù)值
- nodejs URL模塊操作URL相關(guān)方法介紹
- nodejs中轉(zhuǎn)換URL字符串與查詢(xún)字符串詳解
- NodeJS學(xué)習(xí)筆記之(Url,QueryString,Path)模塊
- node.js中的url.format方法使用說(shuō)明
- node.js中的url.resolve方法使用說(shuō)明
- NodeJS的url截取模塊url-extract的使用實(shí)例
- nodejs入門(mén)教程四:URL相關(guān)模塊用法分析
- Node.js學(xué)習(xí)之地址解析模塊URL的使用詳解
相關(guān)文章
想學(xué)習(xí)javascript JS和jQuery哪個(gè)重要 先學(xué)哪個(gè)
在一些技術(shù)論壇與qq群經(jīng)??吹接羞@樣類(lèi)似的提問(wèn),當(dāng)然提出這樣問(wèn)題的通常都是新手為了解決大家的疑惑,同時(shí)幫助新手程序員能更快掌握學(xué)習(xí)的方向,不致于弄錯(cuò)重點(diǎn)2016-12-12Javascript入門(mén)學(xué)習(xí)第二篇 js類(lèi)型
上篇文章講了js中的一些概念(詞法結(jié)構(gòu)) 和 數(shù)據(jù)類(lèi)型(部分)。 這章我們 繼續(xù).然后了解下js中操作數(shù)據(jù) 和 函數(shù)的 作用域。2008-07-07Javascript基礎(chǔ)教程之定義和調(diào)用函數(shù)
這篇文章主要介紹了Javascript基礎(chǔ)教程之定義和調(diào)用函數(shù)的相關(guān)資料,需要的朋友可以參考下2015-01-01javascript將相對(duì)路徑轉(zhuǎn)絕對(duì)路徑示例
這篇文章主要介紹了javascript將相對(duì)路徑轉(zhuǎn)絕對(duì)路徑示例,這里介紹的其實(shí)本質(zhì)上是兩種方法,通過(guò)創(chuàng)建DOM或通過(guò)JavaScript計(jì)算,需要的朋友可以參考下2014-03-03