是否可以在Express.js中命名路由 [英] is it possible to name routes in Express.js
问题描述
app.get('/',function(req,res){
res.send('hello world');
});
是否可以命名该路由并将其提供给任何模板,以便可以像这样使用:
app.get('/',name =index,function(req,res){
res.send('hello world');
});
转到站点< a href ={%url index%}>索引页< / a> ;.
灵感来自Django:)
没有开箱即用的机制。但是,您可以模仿Django的风格:定义 urls.js
文件,该文件将包含一组URL。首先从:
myviews.js
exports.Index = function(req,res,next){
res.send(hello world!);
};
urls.js
var MyViews = require(mywviews.js);
module.exports = [
{name:index,pattern:/,view:MyViews.Index}
]
现在,在 app.js (或任何主要文件)中,您需要将URL绑定到Express。例如:
app.js
var urls = require(urls.js); $($)
for(var i = 0,l = urls.length; i< l; i ++){
var url = urls [i];
app.all(url.pattern,url.view);
};
现在您可以定义自定义助手(Express 3.0风格):
var urls = require(urls.js),l = urls.length;
app.locals.url = function(name){
for(var i = 0; i< l; i ++){
var url = urls [i];
if(url.name === name){
return url.pattern;
}
};
};
,您可以轻松地在模板中使用它。现在的问题是,它不会给你像Django这样的花哨的URL创建机制(你可以将其他参数传递给 url
)。另一方面,您可以修改 url
函数并扩展它。我不想在这里进入所有的细节,但下面是一个例子,如何使用正则表达式(你应该可以将它们结合在一起):
< a href =https://stackoverflow.com/questions/10027574/express-js-reverse-url-route-django-style> Express JS反向URL路由(Django样式)
请注意,我发布了这个问题,所以我之前有同样的问题。 :D
Basic route is like this:
app.get('/', function(req, res){
res.send('hello world');
});
Is it possible to name that route and have it available in any template so it can be used like this:
app.get('/', name="index", function(req, res){
res.send('hello world');
});
Go to site <a href="{% url index %}">index page</a>.
Inspiration comes from Django :)
There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js
file which will hold an array of URLs. First start with:
myviews.js
exports.Index = function( req, res, next ) {
res.send( "hello world!" );
};
urls.js
var MyViews = require( "mywviews.js" );
module.exports = [
{ name : "index", pattern : "/", view : MyViews.Index }
]
Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:
app.js
var urls = require( "urls.js" );
for ( var i = 0, l = urls.length; i < l; i++ ) {
var url = urls[ i ];
app.all( url.pattern, url.view );
};
Now you can define custom helper ( Express 3.0 style ):
var urls = require( "urls.js" ), l = urls.length;
app.locals.url = function( name ) {
for ( var i = 0; i < l; i++ ) {
var url = urls[ i ];
if ( url.name === name ) {
return url.pattern;
}
};
};
and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url
). On the other hand you can modify url
function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):
Express JS reverse URL route (Django style)
Note that I posted the question, so I had the same problem some time ago. :D
这篇关于是否可以在Express.js中命名路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!