【问题标题】:Node.js: how to get the URL to the page which called a POST from the request body?Node.js:如何从请求正文中获取调用 POST 的页面的 URL?
【发布时间】:2018-12-07 12:01:12
【问题描述】:

我正在 Node.js 上构建一个 ExpressJS Web 应用程序。

我有一个通过 GET 路由呈现的页面:“http://localhost:3000/items/showItemList?status=0”。

此页面调用 POST:“http://localhost:3000/items/approve”。

在 POST 路由中,我想获取指向原始页面“http://localhost:3000/items/showItemList?status=0”的 URL 的 URL。

router.post('/approve', async function(req, res, next) {
  // How to get the Url to the originating page.
});

【问题讨论】:

  • 当 POST 被触发回到 GET 路由时,你的意思是像重定向一样吗?

标签: javascript node.js express


【解决方案1】:

你可以像这样从req参数获取

req.headers.referer

【讨论】:

    【解决方案2】:

    const host = req.get('host') 或者const origin = req.get('origin')

    这样使用:

    router.post('/approve', async function(req, res, next) {
      const origin = req.get('origin')
    })
    

    【讨论】:

    • 如果是重定向,则可以是return res.redirect('http://localhost:3000/items/showItemList?status=0');
    • 我不认为它是重定向。他正在收到一个 POST 请求,可能来自 ajax,他想知道发送请求的页面的 URL
    • req.get('origin') 给了我 undefined 而 req.get('host') 给了我 localhost:3000。他们都没有给我确切的网址。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2011-12-27
    • 2019-11-26
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多