在express.js上启用HTTPS [英] Enabling HTTPS on express.js

查看:109
本文介绍了在express.js上启用HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的 app.js 代码。

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

var privateKey = fs.readFileSync('sslcert / server.key');
var certificate = fs.readFileSync('sslcert / server.crt');

var credentials = {key:privateKey,cert:certificate};


var app = express.createServer(credentials);

app.get('/',function(req,res){
res.send('hello');
});

app.listen(8000);

当我运行它,似乎只响应HTTP请求。



我写了简单的vanilla node.js 基于HTTPS应用程序:

  var fs = require(fs),
http = require(https);

var privateKey = fs.readFileSync('sslcert / server.key')。toString();
var certificate = fs.readFileSync('sslcert / server.crt')。toString();

var credentials = {key:privateKey,cert:certificate};

var server = http.createServer(credentials,function(req,res){
res.writeHead(200,{'Content-Type':'text / plain'});
res.end('Hello World\\\
');
});

server.listen(8000);

当我运行这个应用程序时,它 回应HTTPS请求。请注意,我不认为fs结果中的toString()很重要,因为我使用了两者的组合,仍然没有es bueno。






编辑添加:



对于生产系统,您最好使用Nginx或HAProxy代替对nodejs应用的请求。您可以设置nginx来处理ssl请求,只需向您的节点app.js. http。



编辑添加(4/6/2015)



对于使用AWS的系统,您最好使用EC2弹性负载平衡器来处理SSL终止,并允许对EC2 Web服务器进行常规的HTTP流量。为了进一步的安全性,设置安全组,以便只允许ELB向EC2实例发送HTTP流量,这将阻止外部未加密的HTTP流量撞击您的计算机。





解决方案

在express.js(自版本3以来),您应该使用该语法:



< pre class =lang-js prettyprint-override> var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('sslcert / server.key','utf8');
var certificate = fs.readFileSync('sslcert / server.crt','utf8');

var credentials = {key:privateKey,cert:certificate};
var express = require('express');
var app = express();

//您的快速配置这里

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials,app);

httpServer.listen(8080);
httpsServer.listen(8443);

以这种方式,您可以向本机http / https服务器提供快捷中间件$ /
$ b

如果您希望应用程序在低于1024的端口上运行,则需要使用 sudo 命令(不推荐)或使用反向代理(例如nginx,haproxy)。


I'm trying to get HTTPS working on express.js for node, and I can't figure it out.

This is my app.js code.

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

var privateKey = fs.readFileSync('sslcert/server.key');
var certificate = fs.readFileSync('sslcert/server.crt');

var credentials = {key: privateKey, cert: certificate};


var app = express.createServer(credentials);

app.get('/', function(req,res) {
    res.send('hello');
});

app.listen(8000);

When I run it, it seems to only respond to HTTP requests.

I wrote simple vanilla node.js based HTTPS app:

var   fs = require("fs"),
      http = require("https");

var privateKey = fs.readFileSync('sslcert/server.key').toString();
var certificate = fs.readFileSync('sslcert/server.crt').toString();

var credentials = {key: privateKey, cert: certificate};

var server = http.createServer(credentials,function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

server.listen(8000);

And when I run this app, it does respond to HTTPS requests. Note that I don't think the toString() on the fs result matters, as I've used combinations of both and still no es bueno.


EDIT TO ADD:

For production systems, you're probably better off using Nginx or HAProxy to proxy requests to your nodejs app. You can setup nginx to handle the ssl requests and just speak http to your node app.js.

EDIT TO ADD (4/6/2015)

For systems on using AWS, you are better off using EC2 Elastic Load Balancers to handle SSL Termination, and allow regular HTTP traffic to your EC2 web servers. For further security, setup your security group such that only the ELB is allowed to send HTTP traffic to the EC2 instances, which will prevent external unencrypted HTTP traffic from hitting your machines.


解决方案

In express.js (since version 3) you should use that syntax:

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// your express configuration here

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

In that way you provide express middleware to the native http/https server

If you want your app running on ports below 1024, you will need to use sudo command (not recommended) or use a reverse proxy (e.g. nginx, haproxy).

这篇关于在express.js上启用HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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