简体   繁体   English

HTTP和HTTPS同时(Express.Io)

[英]HTTP and HTTPS same time (Express.Io)

I'm successfully listenin port 443 and can access server over https , but I can't access it with http . 我已成功侦听端口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);

I've tried using http and https modules: 我尝试使用http和https模块:

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

But this time socket.io is giving 404 in browser. 但是这次socket.io在浏览器中提供了404。 How can I solve this? 我该如何解决? I need to use Express.Io 's socket connection because application is based on it. 我需要使用Express.Iosocket连接,因为应用程序基于它。

You should redirect http to https 您应该将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)
 });

Had the same problem a few days ago, and this GitHub issue helped: https://github.com/techpines/express.io/issues/17#issuecomment-26191447 几天前发生了同样的问题,这个GitHub问题帮助了: https : //github.com/techpines/express.io/issues/17#issuecomment-26191447

Your code is on the right way, it just need some changes. 您的代码采用正确的方式,只需要进行一些更改。 The code below is a slightly modified version of the snipped you provided. 下面的代码是您提供的片段的略微修改版本。

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() { });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM