简体   繁体   English

侦听单个 Express 应用程序的 HTTP 和 HTTPS

[英]Listen on HTTP and HTTPS for a single express app

Can I create an Express server listening on both HTTP and HTTPS, with the same routes and the same middlewares?我可以使用相同的路由和相同的中间件创建一个同时侦听 HTTP 和 HTTPS 的 Express 服务器吗?

Currently I do this with Express on HTTP, with stunnel tunneling HTTPS to Express, but I prefer a pure Node solution.目前,我在 HTTP 上使用 Express 执行此操作,使用stunnel隧道将 HTTPS 传输到 Express,但我更喜欢纯 Node 解决方案。

I can do it with this code, but using the handle method that is marked as private:我可以用这段代码来做,但使用标记为私有的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 );

To enable your app to listen for both http and https on ports 80 and 443 respectively, do the following要让您的应用分别在端口80443上侦听httphttps ,请执行以下操作

Create an express app:创建一个快速应用:

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

The app returned by express() is a JavaScript function. express()返回的应用程序是一个 JavaScript 函数。 It can be be passed to Node's HTTP servers as a callback to handle requests.它可以作为回调传递给 Node 的 HTTP 服务器来处理请求。 This makes it easy to provide both HTTP and HTTPS versions of your app using the same code base.这使得使用相同的代码库轻松提供应用程序的 HTTP 和 HTTPS 版本。

You can do so as follows:你可以这样做:

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'),
  ca: fs.readFileSync('/path/to/ca.pem')
};

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

For complete detail see the doc有关完整的详细信息,请参阅文档

As a possible update to this question, you might want to check out the changes here for express 3. The change document says:作为对这个问题的可能更新,您可能想在此处查看 express 3 的更改。更改文档说:

The return value of express() is a JavaScript Function, encapsulating everything that makes an Express app tick. express()的返回值是一个 JavaScript 函数,它封装了使 Express 应用程序运行的所有内容。 This means you can easily setup HTTP and HTTPS versions of your application by passing it to node's http.createServer() and https.createServer() :这意味着您可以通过将应用程序传递给节点的http.createServer()https.createServer()来轻松设置应用程序的 HTTP 和 HTTPS 版本:

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

Here is a complete example for express 3:这是 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);

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

You can use express and https in same port.您可以在同一端口使用 express 和 https。

this works for me.这对我有用。

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

If you want to use the traditional two ports, one of the above solutions probably works, but using httpolyglot, you can really easily have http and https on the same port with the same middlewares.如果您想使用传统的两个端口,上述解决方案之一可能有效,但是使用 httpolyglot,您可以使用相同的中间件轻松地在同一端口上使用 http 和 https。

https://github.com/mscdex/httpolyglot https://github.com/mscdex/httpolyglot

Here's some skeleton code that worked for me:这是一些对我有用的骨架代码:

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

and if you want http -> https forwarding, you can just add this middleware function before the createServer() call:如果你想要 http -> https 转发,你可以在 createServer() 调用之前添加这个中间件函数:

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

This can be set up on a custom port这可以在自定义端口上设置

Similar post类似帖子

Can I configure expressjs to serve some pages over http and others over https? 我可以将 expressjs 配置为通过 http 提供某些页面,而通过 https 提供其他页面吗?

Be aware that express now support creating Https servers with:请注意,express 现在支持通过以下方式创建 Https 服务器:

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

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

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