【问题标题】:Node: How to avoid hardcoding URLs in views?节点:如何避免在视图中硬编码 URL?
【发布时间】:2014-03-07 15:13:30
【问题描述】:

我需要避免在 Node 应用程序(代码或视图)中编写硬编码 URls。 有包吗?

现在我用 Jade 写: a(href='/account/profile') Profile 个人资料网址是硬编码的。 我需要这样的东西: a(href=links.accounts.profile()) Profile

当我更改 URL 时它会非常有用。我需要能够仅从配置文件更改 URL,而不是从所有视图...

【问题讨论】:

  • 解决方案必须适用于外部静态文件,而不仅仅是应用程序路由。例如,如果我为静态资源更改主机,我只需要在 config/setup 中更改主机 - 而不是在所有视图中!

标签: node.js url hyperlink pug hardcode


【解决方案1】:

你可以使用这个包 - reversable-router.

他们自述文件中的示例:

app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
});

//.. and a helper in the view files:
url('admin.user.edit', {id: 2})

【讨论】:

  • 这个方法也是硬编码的。但这是一个不错的方法。
猜你喜欢
  • 2022-10-31
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多