在单个快递应用上收听HTTP和HTTPS

时间:2011-12-02 11:12:56

标签: javascript node.js https express

我是否可以使用相同的路由和相同的中间件创建一个同时监听HTTP和HTTPS的Express服务器?

目前我使用Express on HTTP,使用stunnel隧道连接到Express,但我更喜欢纯粹的Node解决方案。

我可以使用此代码执行此操作,但使用标记为私有的handle方法:

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 );

6 个答案:

答案 0 :(得分:35)

作为此问题的可能更新,您可能需要查看快递3的更改here。更改文档说明:

  

express()的返回值是一个JavaScript函数,   封装使Express应用程序打勾的所有内容。这意味着   您可以通过以下方式轻松设置应用程序的HTTP和HTTPS版本   将其传递给节点的http.createServer()https.createServer()

在Express 3中,express.createServer()现在是express()

以下是快递3的完整示例:

var fs = require('fs')
    , https = require('https')
    , http = require('http')
    , express = require('express')
    , keys_dir = 'keys/'
    , server_options = {
        key  : fs.readFileSync(keys_dir + 'privatekey.pem'),
        ca   : fs.readFileSync(keys_dir + 'certauthority.pem'),
        cert : fs.readFileSync(keys_dir + 'certificate.pem')
      }
    , app = express();
app.configure(function(){
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.session( { secret: '' } ));
  app.use(app.router);
});
app.configure('development',function(){
  app.use(express.static(__dirname + '/public'));
  app.use(express.errorHandler({dumpExceptions: true, showStack:true}));
  app.set('view options', { pretty: true });
});
app.get('/', function(req, res){
  res.send('Hello World!');
});
https.createServer(server_options,app).listen(7000);
http.createServer(app).listen(8000);

答案 1 :(得分:29)

要让您的应用分别在端口httphttps上同时收听80443,请执行以下操作

创建快速应用:

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

express()返回的应用是JavaScript功能。它可以作为回调来传递给Node的HTTP服务器来处理请求。这样可以使用相同的代码库轻松提供应用的HTTP和HTTPS版本。

您可以按照以下方式执行此操作:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();

var options = {
  key: fs.readFileSync('/path/to/key.pem'),
  cert: fs.readFileSync('/path/to/cert.pem')
};

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

有关完整详细信息,请参阅doc

答案 2 :(得分:23)

您可以通过以下方式分享实施:

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);

答案 3 :(得分:1)

您可以在同一端口中使用express和https。

这对我有用。

const express=require('express');
const app=express();
const cors=require('cors');
const path=require("path");
const routes=require('./routes/api');
const routerComplain=require('./routes/api');
const routerstores=require('./routes/api');
const routerstock=require('./routes/api');
const routerreport=require('./routes/api');
const routeritem=require('./routes/api');
const bodyParser=require('body-parser');
const routerRegister=require('./routes/api');
const mongoose=require('mongoose');
var server = require('http').Server(app);
var io = require('socket.io')(server);
require("dotenv").config();

mongoose.connect('mongodb://@@@@@@@@@@@@@@@@@',{ useNewUrlParser: true },(err)=>{
    if(!err){
        console.log('db connected')
    }else{
        console.log('error in db')
    }
});

mongoose.Promise = global.Promise;
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(cors({credentials: true, origin:'http://localhost:3000'}));
app.use(express.static(path.join(__dirname, "client", "build")))

app.use('/reg',routes);
app.use('/complain',routerComplain);
app.use('/register',routerRegister);
app.use('/stores',routerstores);
app.use('/reports',routerreport);
app.use('/stock',routerstock);
app.use('/items',routeritem);

app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
      console.log(data);
    });
  })

const port = process.env.port||4000;

server.listen(port,function(){
    console.log('now listening for request');
});

答案 4 :(得分:0)

类似的帖子

Can I configure expressjs to serve some pages over http and others over https?

请注意,express现在支持使用以下命令创建Https服务器:

 var app = require('express').createServer({ key: ... });

答案 5 :(得分:0)

如果要使用传统的两个端口,上述解决方案之一可能会起作用,但是使用httpolyglot,您可以轻松地在具有相同中间件的同一端口上使用http和https。

https://github.com/mscdex/httpolyglot

以下一些对我有用的基本代码:

var express = require('express');
var fs = require('fs');
var httpolyglot = require('httpolyglot');
var app = express();

const options = {
    key: fs.readFileSync("/etc/ssl/certs/key"),
    cert: fs.readFileSync("/etc/ssl/certs/cer.cer")
};

httpolyglot.createServer(options, app).listen(port);

,如果要http-> https转发,则可以在createServer()调用之前添加此中间件功能:

app.use(function(req, res, next) {
    if (!req.secure ) {
            res.redirect (301, 'https://' + req.hostname + ':port' + req.originalUrl);
    }
    next();
});

这可以在自定义端口上设置