Sui*_*sse 2 javascript ssl node.js express sails.js
我尝试强制我在 heroku 上托管的 Sails.js WebApp(未安装 nginx)从 http:// 到 https:// 并在我的 Sailing.js 应用程序中使用这个express 中间件:
在 Express 中,它看起来像这样:
app.use(forceDomain({
hostname: 'www.example.com',
port: 4000,
protocol: 'https'
}));
Run Code Online (Sandbox Code Playgroud)
我尝试在我的 Sails.js 应用程序的 config/http.js 文件中使用它:
middleware: {
forceDomain: function (req, res, next) {
forceDomain({
hostname: 'www.myurl.com',
port: 4000,
protocol: 'https'
});
next();
},
order: [
'forceDomain',
...
}
Run Code Online (Sandbox Code Playgroud)
我不明白如何在sails.js 中使用这个“app.use()”这个东西。据这里解释,但我真的不明白。我现在所拥有的不起作用(没有错误,但也没有重定向)。我怎样才能解决这个问题?
安装了这个模块- 也不起作用。
这是如何在没有 nginx 和外部模块的情况下在 heroku 上运行的sails.js 应用程序上强制使用 ssl 的解决方案:
在 config/http.js 文件中有一个自定义中间件的例子:
****************************************************************************
* Example custom middleware; logs each request to the console. *
****************************************************************************
myRequestLogger: function (req, res, next) {
console.log("Requested :: ", req.method, req.url);
next();
}
Run Code Online (Sandbox Code Playgroud)
我制作了自己的自定义中间件函数,该函数控制请求是否安全,如果不安全,它将请求重定向到 https:// 或者如果它是 websocket 请求,它将请求重定向到 wss://
order: [
...
'forceSSL',
...
],
forceSSL: function (req, res, next) {
if (req.isSocket) {
return res.redirect('wss://' + req.headers.host + req.url);
} else if (req.headers["x-forwarded-proto"] == "http") {
return res.redirect('https://' + req.headers.host + req.url);
} else {
next(); //it's already secure
}
}
Run Code Online (Sandbox Code Playgroud)
不需要外部模块或连接。只是那个功能,它的工作原理。
| 归档时间: |
|
| 查看次数: |
1138 次 |
| 最近记录: |