简体   繁体   English

如何将用户的浏览器重定向到外部链接

[英]How to redirect user's browser to external link

I'm new to node.js and javascript.我是 node.js 和 javascript 的新手。 I want to redirect the user's browser using a function after doing some calculations in node.js.我想在 node.js 中进行一些计算后使用函数重定向用户的浏览器。

here's my code这是我的代码

 const express = require('express'); const app = express(); app.get('/number/:num', (req, res) =>{ var user_num = req.params.num; user_num = user_num + 10; // here, I do some calculations like this go(user_num) }); function go(nu){ var number_2 = nu + "1"; // some calculations here too. //here I want to redirect user to a url with the number_2 on the url. // www.mydomain.com/number/?id=number_2 <==== LIKE THIS } app.listen(3000, () => console.log('listening to port 3000...'));

Is there any method to redirect the user like that?有没有什么方法可以像这样重定向用户? Please help.请帮忙。 Thanks谢谢

res.redirect(`/number/?id=${number_2}`);

This should do the trick if you want to redirect to a local url.如果您想重定向到本地 url,这应该可以解决问题。

res.status(301).redirect(`www.mydomain.com/number/?id=${number_2}`);

This will redirect to an external url.这将重定向到外部 url。 I suggest reading into Express docs and HTTP statuses .我建议阅读Express docsHTTP statuses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM