一直很喜欢restify的轻量级,只是不支持all的方法,又不想像引入express(不想要模版引擎), 于是决定深入研究一下restify的源码,发现只需要简单改几行就可以
1.\node_modules\restify\lib\server.js
// Register all the routing methods
/**
* Mounts a chain on the given path against this HTTP verb
*
* @param {Object} options the URL to handle, at minimum.
* @return {Route} the newly created route.
*/
[
'del',
'get',
'head',
'opts',
'post',
'put',
'patch',
'all' /*haiyang*/
].forEach(function (method) {
Server.prototype[method] = function (opts) {
if (opts instanceof RegExp || typeof (opts) === 'string') {
opts = {
path: opts
};
} else if (typeof (opts) === 'object') {
opts = shallowCopy(opts);
} else {
throw new TypeError('path (string) required');
}
2.\node_modules\restify\lib\router.js
// A list of methods to routes
this.routes = {
DELETE: [],
GET: [],
HEAD: [],
OPTIONS: [],
PATCH: [],
POST: [],
PUT: [],
ALL: [] /*haiyang*/
};
....
Router.prototype.find = function find(req, res, callback) {
var candidates = [];
var ct = req.headers['content-type'] || DEF_CT;
var cacheKey = req.method + req.url + req.version() + ct;
var cacheVal;
var neg;
var params;
var r;
var reverse;
var routes = (this.routes['ALL']||[]).concat(this.routes[req.method] || []); /*haiyang*/
var typed;
var versioned;