【问题标题】:How to redirect to a vuejs front-end running on port 8080 from Nodejs backend on port 3000 along with data如何从端口 3000 上的 Nodejs 后端以及数据重定向到在端口 8080 上运行的 vuejs 前端
【发布时间】:2020-10-31 20:32:56
【问题描述】:

我可以使用重定向

res.writeHead(302, {
    Location: 'http://localhost:8080/path'
})

但我需要在重定向的同时发送一些数据。我该怎么做?

【问题讨论】:

    标签: node.js vue.js redirect vue-cli


    【解决方案1】:

    我认为 listen() 方法是你需要的 nodejs 后端服务器。

    var http = require('http'); 
    var url = require('url'); 
    
    http.createServer(function (req, res) { 
     console.log("Request: " + req.method + " to " + req.url); 
     res.writeHead(200, "OK"); 
     res.write("<h1>Hello</h1>Node.js is listening new port"); 
     res.end(); 
    }).listen(3000); 
    

    线程:Changing Node.js listening port

    【讨论】:

      【解决方案2】:

      从重定向传递数据的一种方法是将其作为查询参数,例如

      res.writeHead(302, {
          Location: 'http://localhost:8080/path?data=1'
      })
      

      在您的 vue 应用上,您可以通过以下方式获取查询参数:

      this.$route.query.data
      

      请记住,如果您传递敏感数据,这并不安全,不建议这样做。

      编辑:

      如果适合您的需要,您也可以将数据作为路径参数传递,例如您的 vue 路由如下:

      routes: [
          {
            path: '/path/:data',
            name: 'path',
            component: PathPage,
          },
      ]
      

      您可以像这样从您的快递应用重定向:

      res.writeHead(302, {
          Location: 'http://localhost:8080/path/50'
      })
      

      你可以像这样从你的 vue 组件中获取参数:

      this.$route.params.data // 50
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-16
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2018-09-21
        • 1970-01-01
        • 2019-05-27
        • 2012-09-29
        相关资源
        最近更新 更多