I am using node and express. To register a controller I call:
我正在使用节点和表达。要注册控制器我打电话:
app.get('/user/:id', function (req, res) {...});
But I would like to do it the rfc-6570 way:
但我想用rfc-6570方式做到这一点:
app.get('/user/{id}', function (req, res) {...});
I googled just an implementation in python on google code, but found nothing (except the dead link on google code to http://www.snellspace.com/wp/?p=831) for JavaScript.
我在google代码上搜索了python中的一个实现,但是没有找到任何内容(除了google代码上的死链接到http://www.snellspace.com/wp/?p=831)。
URI templating in general is not so easy as it looks on the first sight. Have a look on the examples in the RFC.
一般来说,URI模板并不像第一眼看上去那么容易。看看RFC中的示例。
PS: I will need the URI templates on the client, too.
PS:我也需要客户端上的URI模板。
3 个解决方案
#1
7
I've been cleaning up the implementations list at http://code.google.com/p/uri-templates/wiki/Implementations - there is a JS one at https://github.com/marc-portier/uri-templates but I'm not sure of whether it implements the RFC, nor of what its quality is.
我一直在清理http://code.google.com/p/uri-templates/wiki/Implementations上的实施列表 - 在https://github.com/marc-portier/uri-上有一个JS模板,但我不确定它是否实现RFC,也不确定它的质量。
Note that we've started publishing tests here: https://github.com/uri-templates/uritemplate-test
请注意,我们已经开始在这里发布测试:https://github.com/uri-templates/uritemplate-test
So if you want to check it, you could start there.
所以,如果你想检查它,你可以从那里开始。
#2
4
As of June 2014, these JavaScript implementations seem most complete (Level 4 of the spec) and tested. All three also support both the browser and node.js.
截至2014年6月,这些JavaScript实现似乎最完整(规范的第4级)并经过测试。这三个还支持浏览器和node.js.
-
fxa/uritemplate-js:
npm install uritemplate
- fxa / uritemplate-js:npm install uritemplate
-
geraintluff/uri-templates (does also variable extraction aka de-substitution):
npm install uri-templates
- geraintluff / uri-templates(也可变提取又称去替换):npm install uri-templates
- URI.js includes URI-Templates:
npm install URIjs
orbower install uri.js
- URI.js包括URI-Templates:npm install URIjs或bower install uri.js
#3
0
Regarding the express router part I would recommend to use your uri templates within a hyperschema (read more) ...
关于快速路由器部分,我建议在超级模式中使用你的uri模板(阅读更多)...
Then you could also benefit from regex for your router which express.js supports. Regarding resolving the parameters you need an RFC 6570 implementation like https://github.com/geraintluff/uri-templates ...
然后你也可以从express.js支持的路由器的正则表达式中受益。关于解析参数,您需要RFC 6570实现,如https://github.com/geraintluff/uri-templates ...
Here is some .js code to illustrate the rewriting of a hyperschema USING RFC 6570 to convert it to an express js router:
下面是一些.js代码,用于说明使用RFC 6570重写超高速模式以将其转换为快速js路由器:
var hyperschema = { "$schema": "http://json-schema.org/draft-04/hyper-schema", "links": [ { "href": "{/id}{/ooo*}{#q}", "method": "GET", "rel": "self", "schema": { "type": "object", "properties": { "params": { "type": "object", "properties": { "id": {"$ref": "#/definitions/id"} }, "additionalProperties": false } }, "additionalProperties": true } } ], "definitions": { "id": { "type": "string", "pattern": "[a-z]{0,3}" } } }
var deref = require('json-schema-deref');
var tv4 = require('tv4');
var url = require('url');
var rql = require('rql/parser');
// DOJO lang AND _
function getDottedProperty(object, parts, create) {
var key;
var i = 0;
while (object && (key = parts[i++])) {
if (typeof object !== 'object') {
return undefined;
}
object = key in object ? object[key] : (create ? object[key] = {} : undefined);
}
return object;
}
function getProperty(object, propertyName, create) {
return getDottedProperty(object, propertyName.split('.'), create);
}
function _rEscape(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function getPattern(k, ldo, customCat) {
// ...* = explode = array
// ...: = maxLength
var key = ((k.slice(-1) === '*') ? k.slice(0,-1) : k).split(':')[0];
var cat = (customCat) ? customCat : 'params'; // becomes default of customCat in TS
var pattern = '';
if (typeof ldo === 'object' && ldo.hasOwnProperty('schema')) {
var res = getProperty(ldo.schema, ['properties',cat,'properties',key,'pattern'].join('.'));
if (res) {
console.log(['properties',cat,'properties',key,'pattern'].join('.'),res);
return ['(',res,')'].join('');
}
}
return pattern;
}
function ldoToRouter(ldo) {
var expression = ldo.href.replace(/(\{\+)/g, '{') // encoding
.replace(/(\{\?.*\})/g, '') // query
.replace(/\{[#]([^}]*)\}/g, function(_, arg) {
// crosshatch
//console.log(arg);
return ['(?:[/]*)?#:',arg,getPattern(arg,ldo,'anchor')].join('');
})
.replace(/\{([./])?([^}]*)\}/g, function(_, op, arg) {
// path seperator
//console.log(op, '::', arg, '::', ldo.schema);
return [op,':',arg,getPattern(arg,ldo)].join('');
});
return {method: ldo.method.toLowerCase(), args:[expression]};
}
deref(hyperschema, function(err, fullSchema) {
console.log('deref hyperschema:',JSON.stringify(fullSchema));
var router = fullSchema.links.map(ldoToRouter);
console.log('router:',JSON.stringify(router));
});
#1
7
I've been cleaning up the implementations list at http://code.google.com/p/uri-templates/wiki/Implementations - there is a JS one at https://github.com/marc-portier/uri-templates but I'm not sure of whether it implements the RFC, nor of what its quality is.
我一直在清理http://code.google.com/p/uri-templates/wiki/Implementations上的实施列表 - 在https://github.com/marc-portier/uri-上有一个JS模板,但我不确定它是否实现RFC,也不确定它的质量。
Note that we've started publishing tests here: https://github.com/uri-templates/uritemplate-test
请注意,我们已经开始在这里发布测试:https://github.com/uri-templates/uritemplate-test
So if you want to check it, you could start there.
所以,如果你想检查它,你可以从那里开始。
#2
4
As of June 2014, these JavaScript implementations seem most complete (Level 4 of the spec) and tested. All three also support both the browser and node.js.
截至2014年6月,这些JavaScript实现似乎最完整(规范的第4级)并经过测试。这三个还支持浏览器和node.js.
-
fxa/uritemplate-js:
npm install uritemplate
- fxa / uritemplate-js:npm install uritemplate
-
geraintluff/uri-templates (does also variable extraction aka de-substitution):
npm install uri-templates
- geraintluff / uri-templates(也可变提取又称去替换):npm install uri-templates
- URI.js includes URI-Templates:
npm install URIjs
orbower install uri.js
- URI.js包括URI-Templates:npm install URIjs或bower install uri.js
#3
0
Regarding the express router part I would recommend to use your uri templates within a hyperschema (read more) ...
关于快速路由器部分,我建议在超级模式中使用你的uri模板(阅读更多)...
Then you could also benefit from regex for your router which express.js supports. Regarding resolving the parameters you need an RFC 6570 implementation like https://github.com/geraintluff/uri-templates ...
然后你也可以从express.js支持的路由器的正则表达式中受益。关于解析参数,您需要RFC 6570实现,如https://github.com/geraintluff/uri-templates ...
Here is some .js code to illustrate the rewriting of a hyperschema USING RFC 6570 to convert it to an express js router:
下面是一些.js代码,用于说明使用RFC 6570重写超高速模式以将其转换为快速js路由器:
var hyperschema = { "$schema": "http://json-schema.org/draft-04/hyper-schema", "links": [ { "href": "{/id}{/ooo*}{#q}", "method": "GET", "rel": "self", "schema": { "type": "object", "properties": { "params": { "type": "object", "properties": { "id": {"$ref": "#/definitions/id"} }, "additionalProperties": false } }, "additionalProperties": true } } ], "definitions": { "id": { "type": "string", "pattern": "[a-z]{0,3}" } } }
var deref = require('json-schema-deref');
var tv4 = require('tv4');
var url = require('url');
var rql = require('rql/parser');
// DOJO lang AND _
function getDottedProperty(object, parts, create) {
var key;
var i = 0;
while (object && (key = parts[i++])) {
if (typeof object !== 'object') {
return undefined;
}
object = key in object ? object[key] : (create ? object[key] = {} : undefined);
}
return object;
}
function getProperty(object, propertyName, create) {
return getDottedProperty(object, propertyName.split('.'), create);
}
function _rEscape(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function getPattern(k, ldo, customCat) {
// ...* = explode = array
// ...: = maxLength
var key = ((k.slice(-1) === '*') ? k.slice(0,-1) : k).split(':')[0];
var cat = (customCat) ? customCat : 'params'; // becomes default of customCat in TS
var pattern = '';
if (typeof ldo === 'object' && ldo.hasOwnProperty('schema')) {
var res = getProperty(ldo.schema, ['properties',cat,'properties',key,'pattern'].join('.'));
if (res) {
console.log(['properties',cat,'properties',key,'pattern'].join('.'),res);
return ['(',res,')'].join('');
}
}
return pattern;
}
function ldoToRouter(ldo) {
var expression = ldo.href.replace(/(\{\+)/g, '{') // encoding
.replace(/(\{\?.*\})/g, '') // query
.replace(/\{[#]([^}]*)\}/g, function(_, arg) {
// crosshatch
//console.log(arg);
return ['(?:[/]*)?#:',arg,getPattern(arg,ldo,'anchor')].join('');
})
.replace(/\{([./])?([^}]*)\}/g, function(_, op, arg) {
// path seperator
//console.log(op, '::', arg, '::', ldo.schema);
return [op,':',arg,getPattern(arg,ldo)].join('');
});
return {method: ldo.method.toLowerCase(), args:[expression]};
}
deref(hyperschema, function(err, fullSchema) {
console.log('deref hyperschema:',JSON.stringify(fullSchema));
var router = fullSchema.links.map(ldoToRouter);
console.log('router:',JSON.stringify(router));
});