简体   繁体   中英

How to redirect user's browser to external link

I'm new to node.js and javascript. I want to redirect the user's browser using a function after doing some calculations in 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.

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

This will redirect to an external url. I suggest reading into Express docs and HTTP statuses .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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