如何将用户的浏览器URL重定向到Nodejs中的其他页面? [英] How to redirect user's browser URL to a different page in Nodejs?

查看:364
本文介绍了如何将用户的浏览器URL重定向到Nodejs中的其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在尝试编写的应用程序中,主页面( http:// localhost:8675 )具有以下表格:

In the application I'm trying to write, the main page (http://localhost:8675) has the following form:

<form action='/?joinnew' method='post'>
  <button>Start</button>
</form>

这是server.js中的代码:

Here is the code in 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
...
}}}

我很乐意,如果有办法,我不需要知道主机地址,因为这可能会改变。

I would love if there were a way to do it where I didn't have to know the host address, since that could be changing.

'http'对象是常规需求('http'),不需要('express')。

The 'http' object is a regular require('http'), NOT require('express').

推荐答案

response.writeHead(301,
  {Location: 'http://whateverhostthiswillbe:8675/'+newRoom}
);
response.end();

这篇关于如何将用户的浏览器URL重定向到Nodejs中的其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆