有什么方法可以使用 express js 4 配置节点 js 应用程序,以在 http 协议下提供一些页面,而在 https 中提供其他需要更高安全性的页面?
我描述了我的问题:我正在开发一家在线商店,我想在协议下显示某些页面,例如产品列表或产品详细信息视图http
,以及我认为需要更多安全性的其他页面,例如登录或购物车视图https
.
我已经尝试过该express-force-ssl
模块,但它不起作用。以下代码片段不是来自我的应用程序(太脏),它只是一个示例,它对我不起作用:
var express = require('express');
var forceSSL = require('express-force-ssl');
var fs = require('fs');
var http = require('http');
var https = require('https');
var ssl_options = {
key: fs.readFileSync('./server-private-key.pem'),
cert: fs.readFileSync('./server-certificate.pem'),
ca: fs.readFileSync('./server-certificate-signing-request.pem')
};
var app = express();
var server = http.createServer(app);
var secureServer = https.createServer(ssl_options, app);
app.use(forceSSL);
app.get('/', function (req, res, next) {
res.send('<a href="/user/userA">Hello</a>')
});
app.get('/user/:name', function (req, res, next) {
var user = req.params.name;
res.send('<a href="/login">Hello ' + user + '</a>')
});
app.get('/login', forceSSL, function (req, res, next) {
res.send('<a href="/">Hello</a><br/><a href="/logout">Goodbye</a>')
});
app.get('/logout', forceSSL, function (req, res, next) {
res.send('<a href="/">Hello</a>')
});
secureServer.listen(443)
server.listen(8085)
console.log('server started');
结果是,当我使用 url 启动应用程序时,http://localhost:8085
服务器会自动将其重定向到协议中https://localhost
的所有页面并提供服务。https
我想要的是开始http://localhost:8085
,导航到http://localhost/user/userA
,然后从它转到https://localhost/login
,如果单击“Hello”链接,我想被重定向到http://localhost:8085
。
是否有任何缺少的代码来获得我想要的行为,甚至没有任何其他方式可以在没有express-force-ssl
模块的情况下实现它?