Geo*_*ith 9 javascript https http node.js express
我在node.js上使用express(3.0)框架来路由我的应用程序.
我的大多数应用程序都使用http
协议,但是我只想通过一个特定的路由https
.这是我的API的一部分,负责注册和验证用户.
例如:
app.get('/connect', function(req, res){
// Must be on HTTPS, if not redirect to HTTPS
});
app.post('/connect', function(req, res){
// Must be on HTTPS
});
app.get('/', function(req, res){
// Must be on HTTP
});
app.get('/build', function(req, res){
// Must be on HTTP
});
Run Code Online (Sandbox Code Playgroud)
如何在同一个应用程序中使用两者?我正在努力在野外找到任何这方面的例子.
jos*_*736 10
只需将你的app
(实际上是一个请求处理函数)传递给createServer
of http
和https
.
var express = require('express')
, http = require('http')
, https = require('https')
, app = express();
http.createServer(app);
https.createServer({ ... }, app);
Run Code Online (Sandbox Code Playgroud)
HTTP和HTTPS请求都通过同一个Express应用程序进行路由.在路由处理程序中,要检查请求是否是通过https进行的,请使用req.secure
.
app.get('/route', function(req, res) {
if (req.secure) {
...
} else {
res.redirect(301, 'https://example.com/route');
}
});
Run Code Online (Sandbox Code Playgroud)
作为旁注,现代智慧认为混合的http/https网站不安全.您可以通过要求他们通过SSL登录来保护用户的密码,但随后切换回http以用于后续请求会使攻击者窃取用户的登录cookie 变得微不足道.
考虑通过SSL向登录用户发出所有请求.
归档时间: |
|
查看次数: |
12230 次 |
最近记录: |