可以通过https配置expressjs来为http和其他人提供一些页面吗? [英] Can I configure expressjs to serve some pages over http and others over https?

查看:107
本文介绍了可以通过https配置expressjs来为http和其他人提供一些页面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据对此问题的回复:

如何配置nodejs / expressjs通过https服务页面?

我一直试图设置相当于:

I've been trying to set up the equivalent of:

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

var app = express.createServer();
var appSecure = express.createServer();
var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
appSecure.setSecure(credentials);


app.get('/secretStuff', function(req,res) {
//redirect to https
}

appSecure.get('/secretStuff', function(req, res) {
//show you the secret stuff
}

这是对当前版本的expressjs和node 2.4可以执行的操作吗?

Is this something that's doable with the current release of expressjs and node 2.4?

推荐答案

是的,这可以完成,看起来你已经拥有了大部分的需要,只需在你的app.get处理程序中发送重定向。

Yes, this can be done and it looks like you already have most of what you need. Just send the redirect in your app.get handler

app.get('/secretStuff', function(req,res) {
  res.redirect('https://' + req.header('Host') + req.url);
}

还要确保你做一些类似 app.listen 80) appSecure.listen(443)实际启动相应端口上的服务器,否则请确保使用正确的HTTPS URL端口。对于生产,这个东西通常在您的应用服务器(node.j)之外处理s)与反向代理,如 nginx 。在nginx中执行此操作很简单,这将使您的node.js进程以非root身份运行,并且不需要将客户端直接连接到node.js,而不是像nginx一样强硬,用于提供活动的interect TCP连接(我在这里再说一遍Ryan Dahl)。

Also make sure you do something like app.listen(80) and appSecure.listen(443) to actually start the servers on the appropriate port. Otherwise be sure to construct the HTTPS URL with the correct port. For production, this thing is typically handled outside of your app server (node.js) with a reverse proxy like nginx. It is trivial to do this in nginx which will let your node.js process run as non-root and remove the need to have clients directly connecting to node.js, which is not as battle-hardened as nginx for serving live internect TCP connections (I'm paraphrasing Ryan Dahl himself here).

这篇关于可以通过https配置expressjs来为http和其他人提供一些页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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