Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js HTTP重定向到其他端口中的HTTPS_Node.js_Http_Redirect_Https_Http Redirect - Fatal编程技术网

Node.js HTTP重定向到其他端口中的HTTPS

Node.js HTTP重定向到其他端口中的HTTPS,node.js,http,redirect,https,http-redirect,Node.js,Http,Redirect,Https,Http Redirect,我正在尝试将每个http请求重定向到https服务器。如果https服务器的端口是443。但是,如果我尝试重定向到其他端口,则不会发生这种情况。这是我的密码: http.createServer(function(req,res){ var host = req.headers['host'], url = req.url; res.writeHead(301, {"Location": "https://" + host + ":

我正在尝试将每个http请求重定向到https服务器。如果https服务器的端口是443。但是,如果我尝试重定向到其他端口,则不会发生这种情况。这是我的密码:

http.createServer(function(req,res){
    var host = req.headers['host'],
        url = req.url;
        res.writeHead(301,
            {"Location": "https://" + host + ":"+SERVER.SPORT + url})
        res.end()
}).listen(SERVER.PORT)

https.createServer(SERVER.HTTPS_OPTIONS,app).listen(SERVER.SPORT)

您的
主机
很可能已经包含端口号

如果更改此项,您可以确保:

var host = req.headers['host']
致:

还添加了一些日志记录:

console.log("https://" + host + ":"+SERVER.SPORT + url);
查看您正在构建的URL的外观

您还可以使用
url
模块处理url,而不是手动连接字符串,以避免类似的错误。见:

在任何情况下,添加一些日志记录,以了解您正在构建的URL是什么样子的。还可以使用curl进行测试:

curl -v http://localhost:1234

查看返回的标题。

我在日志中记录了这一点,但没有注意到。在你回答之后,我觉得自己真的很傻,他们甚至在网站上有一个很好的url表示!但是,如果我明确要求使用
example.com:80
,在其他答案中找到的方法(我使用的)也会失败。这意味着这只会因为隐藏的默认端口而起作用?
curl -v http://localhost:1234