Kev*_*gts 5 heroku node.js reactjs
有关我的设置的一般信息
目前,我正在使用React和Nodejs API 构建一个 Web 应用程序,该 API 为该 Web 应用程序提供数据。这两个应用程序都托管在heroku.com上,并且彼此独立运行。我从不同的托管提供商处购买了自定义域,并使用heroku 自定义域选项将 DNS 指向我的网站。
有关我的设置的技术详细信息
- NodeJS 服务器:Express
- NodeJS版本:v10.15.0
- 反应版本:v16.2.0
- 自定义域名:www.tabbs.nl
- Heroku 域名:tabbs-web-app.herokuapp.com
我遇到的问题
我已经深入研究了大量文档和教程,以便为 React / NodeJS 设置 SSL,但找不到关于如何为我的设置设置 SSL / 安全性的不错的教程。
我已经读过的教程:
我想实现什么目标?
我想要实现的目标是在 React Web 应用程序(前端)和 NodeJS API(后端)之间建立安全连接,以便它们之间的所有数据都是加密且安全的。另外,我希望我的自定义域(由与 Heroku 不同的托管提供商购买)是安全的并强制使用 https。
如有任何疑问或其他信息,请随时询问!
小智 1
您是否尝试过在节点中使用 https 模块?
\n\n你可以这样做:
\n\nvar express = require(\'express\');\nvar https = require(\'https\');\nvar http = require(\'http\');\nvar app = express();\n\nhttp.createServer(app).listen(80);\nhttps.createServer(options, app).listen(443);\nRun Code Online (Sandbox Code Playgroud)\n\nexpress() 返回的应用程序实际上是一个 JavaScript 函数,旨在作为回调传递到 Node\xe2\x80\x99s HTTP 服务器来处理请求。这使得您可以轻松地为应用程序的 HTTP 和 HTTPS 版本提供相同的代码库,因为应用程序不会从这些版本继承(它只是一个回调)。
\n\n如果您使用的是 create React app,请打开终端并输入 \xe2\x80\x9cnpm run build\xe2\x80\x9d。这将创建一个包含所有静态文件的构建文件夹。
\n\n现在返回节点后端服务并添加以下内容:
\n\nvar express = require(\'express\');\nvar path = require(\'path\');\nvar https = require(\'https\');\nvar http = require(\'http\');\nvar app = express();\n\nconst options = {\n key: fs.readFileSync("/srv/www/keys/my-site-key.pem"),\n cert: fs.readFileSync("/srv/www/keys/chain.pem")\n};\nhttp.createServer(app).listen(80);\nhttps.createServer(options, app).listen(443);\n\napp.use(express.static(path.join(__dirname, \'build\')));\n\napp.get(\'/\', function(req, res) {\n res.sendFile(path.join(__dirname, \'build\', \'index.html\'));\n});\nRun Code Online (Sandbox Code Playgroud)\n\n如果您\xe2\x80\x99使用React路由器来处理Web应用程序的路由,那么您将修改GET请求,如下所示:
\n\nvar express = require(\'express\');\nconst path = require(\'path\');\nvar https = require(\'https\');\nvar http = require(\'http\');\nvar app = express();\nconst options = {\n key: fs.readFileSync("/srv/www/keys/my-site-key.pem"),\n cert: fs.readFileSync("/srv/www/keys/chain.pem")\n};\nhttp.createServer(app).listen(80);\nhttps.createServer(options, app).listen(443);\n\napp.use(express.static(path.join(__dirname, \'build\')));\n\napp.get(\'/*\', function(req, res) {\n res.sendFile(path.join(__dirname, \'build\', \'index.html\'));\n});\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3077 次 |
| 最近记录: |