如何将用户的浏览器URLredirect到Nodejs中的不同页面?
在我正在编写的应用程序中,主页( http:// localhost:8675 )具有以下forms:
<form action='/?joinnew' method='post'> <button>Start</button> </form>
这里是server.js中的代码:
http.createServer(function(request, response) { var root = url.parse(request.url).pathname.split('/')[1]; if (root == '') { var query = url.parse(request.url).search: if (query == '?joinnew') { var newRoom = getAvaliableRoomId(); // '8dn1u', 'idjh1', '8jm84', etc. // redirect the user's web browser to a new url // ??? How to do. Need to redirect to 'http://whateverhostthiswillbe:8675/'+newRoom ... }}}
如果有办法做到这一点,我不知道主机地址,因为这可能会改变。
'http'对象是一个普通的require('http'),NOT require('express')。
response.writeHead(301, {Location: 'http://whateverhostthiswillbe:8675/'+newRoom} ); response.end();
http.get('*',function(req,res){ res.redirect('http://mydomain.com'+req.url) })
OP:“如果有办法做我不知道主人的地址的话,我会很高兴的……”
response.writeHead(301, { Location: "http" + (request.socket.encrypted ? "s" : "") + "://" + request.headers.host + newRoom }); response.end();