【问题标题】:HTTP and HTTPS same time (Express.Io)同时使用 HTTP 和 HTTPS (Express.Io)
【发布时间】:2015-01-01 22:43:09
【问题描述】:

我在 443 端口监听成功,可以通过 https 访问服务器,但是我无法通过 http 访问。

var fs = require('fs')
options = {
    ca : fs.readFileSync('./ssl/site.com.pem'),
    key: fs.readFileSync('./ssl/site.com.key'),
    cert: fs.readFileSync('./ssl/site_com.crt')
}

var app = require('express.io')
app.https(options).io()
....
app.listen(443);

我尝试过使用 http 和 https 模块:

app.http().io();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);

但这一次socket.io 在浏览器中给出了 404。我该如何解决这个问题?我需要使用Express.Iosocket 连接,因为应用程序是基于它的。

【问题讨论】:

  • 你能添加你的整个代码和相关代码吗?

标签: node.js http express socket.io express.io


【解决方案1】:

几天前遇到了同样的问题,这个 GitHub 问题帮助了: https://github.com/techpines/express.io/issues/17#issuecomment-26191447

您的代码是正确的,它只需要一些更改。下面的代码是您提供的 sn-p 的略微修改版本。

var fs = require('fs'),
    express = require('express.io');

options = {
    ca : fs.readFileSync('./ssl/site.com.pem'),
    key: fs.readFileSync('./ssl/site.com.key'),
    cert: fs.readFileSync('./ssl/site_com.crt')
};

var app = express();
app.https(options).io();
var httpServer = require('http').createServer(app);

// ...

app.listen(443);
express.io.listen(httpServer);
httpServer.listen(80, function() { }, function() { });

【讨论】:

  • 我已经切换到 socket.io + express。基本解决方案。
【解决方案2】:

你应该将 http 重定向到 https

 var express = require('express'),
 app = express(),  
 httpapp = express();

  //........................

 var credentials = {key: privateKey, cert: certificate, ca: ca};
 var httpsServer = https.createServer(credentials, app);
 var httpServer = http.createServer(httpapp);


 httpsServer.listen(443);
 httpServer.listen(80); 


 httpapp.route('*').get(function(req,res){  
    res.redirect('https://yourdomain.com'+req.url)
 });

【讨论】:

  • 我知道,但正如我所说,socket.io 在客户端出现 404 错误。
  • 是的,但是您发布的代码中没有重定向。
  • 我没有添加,谢谢。我的问题是套接字。
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 2016-03-28
  • 2019-08-03
  • 2021-11-07
相关资源
最近更新 更多