在HTTP和HTTPS上侦听单个快速应用程序 [英] Listen on HTTP and HTTPS for a single express app

查看:131
本文介绍了在HTTP和HTTPS上侦听单个快速应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以使用Express在HTTP上进行此操作,还可以在HTTP和HTTPS上创建Express服务器,并使用相同的路由和相同的中间件 stunnel 将HTTPS传送到Express,但我更喜欢纯Node方案。



我可以使用此代码,但使用标记为私有的句柄方法:

 
,fs = require('fs');


var app = express.createServer();
// init路由和中间件
app.listen(80);

var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var options = {key:privateKey,cert:certificate};
https.createServer(options,function(req,res)
{
app.handle(req,res);
}).listen(443);


解决方案

您可以通过以下方式共享实现: p>

  var register = function(app){
// config middleware
app.configure({

});

// config route
app.get(...);
};

var http = express.createServer();
注册(http);
http.listen(80);

var https = express.createServer({key:/ * https properties * /});
注册(https);
https.listen(443);


Can I create an Express server listening on both HTTP and HTTPS, with the same routes and the same middlewares?

Currently I do this with Express on HTTP, with stunnel tunneling HTTPS to Express, but I prefer a pure Node solution.

I can do it with this code, but using the handle method that is marked as private:

var express = require( 'express' )
    , https = require("https")
    , fs = require( 'fs' );

var app = express.createServer();
// init routes and middlewares
app.listen( 80 );

var privateKey = fs.readFileSync( 'privatekey.pem' ).toString();
var certificate = fs.readFileSync( 'certificate.pem' ).toString();
var options = {key: privateKey, cert: certificate};
https.createServer( options, function(req,res)
{
    app.handle( req, res );
} ).listen( 443 );

解决方案

You can share the implementation via something like:

var register = function (app) {
    // config middleware
    app.configure({

    });

    // config routes
    app.get(...);
};

var http = express.createServer();
register(http);
http.listen(80);

var https = express.createServer({ key: /* https properties */ });
register(https);
https.listen(443);

这篇关于在HTTP和HTTPS上侦听单个快速应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆