通配符中的多个呈现器获取nodejs(Multiple renders in a wildcard get nodejs)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
通配符中的多个呈现器获取nodejs(Multiple renders in a wildcard get nodejs)

我正在尝试使用通配符get函数,根据需要呈现视图(我使用jQuery来调用这些视图)。

这是我正在尝试的代码:

app.get('/*.ejs', function(req, res){ // Actions View if (req.xhr) { if ('actions') { res.render('actions'); } else if ('profile') { res.render('profile'); } } else { res.redirect('/game'); } });

我知道这没有任何作用,并且它总是会使我的行为看起来,也许你不能以这种方式开始。 我的问题是:我是不是错了? 我宁愿不为每个我想要的视图创建不同的获取函数。

I am trying to have a wildcard get function that renders a view based on what is needed (I am using jQuery to call these views).

Here is the code I am trying:

app.get('/*.ejs', function(req, res){ // Actions View if (req.xhr) { if ('actions') { res.render('actions'); } else if ('profile') { res.render('profile'); } } else { res.redirect('/game'); } });

I understand that this does no work, and it will always render my actions view, perhaps you can't do it this way to begin with. My question is: am I going about this wrong? I'd rather not create different get functions for each view that I want.

最满意答案

您可以从req.params获取url参数,并构建关于该参数的逻辑。 对于你的情况,它可以是这样的:

app.get('/:view.ejs', function(req, res) { // Actions View if (req.xhr) { if (req.params.view === 'actions') { res.render('actions'); } else if (req.params.view === 'profile') { res.render('profile'); } } else { res.redirect('/game'); } });

You can get url parameter from req.params, and build your logic regarding to that parameter. For your case, it can be something like this:

app.get('/:view.ejs', function(req, res) { // Actions View if (req.xhr) { if (req.params.view === 'actions') { res.render('actions'); } else if (req.params.view === 'profile') { res.render('profile'); } } else { res.redirect('/game'); } });

更多推荐

本文发布于:2023-04-17 08:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6a2c6f3f0ae22eb2fc09ced725b99495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   多个   nodejs   wildcard   renders

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!