Express Node.js 框架:如何通过替换指定参数段之一来重写 URL?

发布于 2024-12-01 17:39:52 字数 809 浏览 4 评论 0原文

在我的 Express 应用程序中,当路由中的某些参数符合某些条件时,我想使用路由参数前置条件执行一些重定向。举个例子,假设我希望将参数 word 转换为全部大写(如果尚未转换)。

参数 word 可能出现在多个不同的路由中,例如 /:word/foo/:word/foo/: word/bar 等。幸运的是,使用路由参数前置条件,这些都由相同的代码处理,例如(CoffeeScript 中的代码):

app.param 'word', (req, res, next, word) ->
  if word isnt word.toUpperCase()
    new_URL = // current URL, but replace word with word.toUpperCase() //
    res.redirect new_URL, 301

我的问题是关于如何构造 new_URL,将 :word 替换为大写 相等的。我希望能够使用相同的函数来构造 new_URL,无论当前使用哪个路由。

我看到有 req.route,但它是未定义的。我想我可以通过req.app.routes.routes.get[req._route_index]访问当前匹配的路由,然后分割path,并重新构建它,但这似乎过于复杂。有没有办法简单地迭代当前路线的 path 段?谢谢!

In my Express app, I'd like to perform some redirects when certain params from the route match certain criteria, using a route param pre-condition. As an example, let's say I want the param word to be converted to all upper-case, if it isn't already.

The param word might be present in several different routes, e.g. /:word, /foo/:word, /foo/:word/bar, etc. Fortunately, with route param pre-conditions, these are all handled by the same code, e.g. (code in CoffeeScript):

app.param 'word', (req, res, next, word) ->
  if word isnt word.toUpperCase()
    new_URL = // current URL, but replace word with word.toUpperCase() //
    res.redirect new_URL, 301

My question regards how to construct new_URL, replacing :word with its upper-case equivalent. I want to be able to use the same function to construct new_URL, regardless of which route is currently being used.

I see that there is req.route, but it is undefined. I guess I can access the currently matched route via req.app.routes.routes.get[req._route_index], and then split the path, and re-construct it, but that seems overly complicated. Is there a way to simply iterate the path segments for the current route? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-12-08 17:39:52

我最终编写了一个小函数来用新值替换命名路由参数。我不确定这是否是最佳解决方案,也不是完整的解决方案(它绝对不适用于名为 params 的 nob-segment,例如 /:from-:to)。

replaceRouteParam = (req, param, replacement) ->
  segments = req.app.routes.routes.get[req._route_index].path.split('/')
  new_segments = []
  for segment in segments
    if segment is param
      new_segments.push replacement
    else if segment[0] is ':'
      new_segments.push req.params[segment[1..]]
    else
      new_segments.push segment

  new_segments.join '/'

我得到的关于在多个位置(我的错误、路由等,位于不同文件中)使用此函数的建议是将它们放在一个单独的文件中,并在需要它的文件中需要它。

I ended up writing a little function to replace a named route parameter with a new value. I'm not sure this is an optimal solution, and it is not a complete solution (it definitely won't work with nob-segment named params like /:from-:to).

replaceRouteParam = (req, param, replacement) ->
  segments = req.app.routes.routes.get[req._route_index].path.split('/')
  new_segments = []
  for segment in segments
    if segment is param
      new_segments.push replacement
    else if segment[0] is ':'
      new_segments.push req.params[segment[1..]]
    else
      new_segments.push segment

  new_segments.join '/'

The advice I got about using this function in multiple locations (my errors, routes, etc, which are in different files) was to put them in a separate file and require it in whichever files need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文