简体   繁体   中英

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 .

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:

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

But this time socket.io is giving 404 in browser. How can I solve this? I need to use Express.Io 's socket connection because application is based on it.

You should redirect http to 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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