![](/img/trans.png)
[英]How do I redirect to front-end (port: 3000) from a back-end (port:3001) route in node/express?
[英]How to redirect to a vuejs front-end running on port 8080 from Nodejs backend on port 3000 along with data
我可以使用重定向
res.writeHead(302, {
Location: 'http://localhost:8080/path'
})
但我需要發送一些數據以及重定向。 我怎樣才能做到這一點?
例如,從重定向傳遞數據的一種方法是將其作為查詢參數
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
我認為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);
主題: 更改Node.js監聽端口
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.