0

我成功监听了 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连接,因为应用程序是基于它的。

4

2 回答 2

0

您应该将 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)
 });
于 2014-11-05T19:22:31.327 回答
0

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

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

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() { });
于 2014-11-16T23:07:41.100 回答