【问题标题】:Deploying ReactJS production build in Express as HTTPS在 Express 中将 ReactJS 生产构建部署为 HTTPS
【发布时间】:2018-11-09 16:13:23
【问题描述】:

所以我在将我的 React 应用程序部署到生产服务器时是成功的。我正在使用 Express 进行部署。这是我的代码。

const express = require('express')
const path = require('path')
const port = process.env.PORT || 80
const app = express()

app.use(express.static(__dirname + '/build'))

app.get('*', function (request, response){
  response.sendFile(path.resolve(__dirname, 'build', 'index.html'))
})

app.listen(port)

我会将 CertBot 用于 SSL 证书并将其部署为 HTTPS 服务器。我真的不知道如何解决这个问题。

【问题讨论】:

标签: node.js reactjs express https certbot


【解决方案1】:

假设 CertBot 生成 SSL 证书和 SSL 证书密钥到以下位置:

  • /etc/letsencrypt/live/example.org/fullchain.pem
  • /etc/letsencrypt/live/example.org/privkey.pem

生成证书和密钥后,您需要直接使用https 模块。

您首先需要导出您的 Express 应用:

const express = require('express')
const path = require('path')
const port = process.env.PORT || 80
const app = express()

app.use(express.static(__dirname + '/build'))

app.get('*', function (request, response){
  response.sendFile(path.resolve(__dirname, 'build', 'index.html'))
})

module.export = app

然后只需导入您的 Express 应用并将其与 https 模块挂钩:

const https = require('https');
const fs = require('fs');
const app = require('./app.js');

const server = https.createServer({
  key: fs.readFileSync('/etc/letsencrypt/live/example.org/fullchain.pem', 'utf8'),
  cert: fs.readFileSync('/etc/letsencrypt/live/example.org/privkey.pem', 'utf8')
}, app);

server.listen(3000);

请注意这基本上是 ./bin/www 为您所做的,但我已将其调整为使用 https 而不是 http 模块。

What does "./bin/www" do in Express 4.x?

【讨论】:

  • tls_common.js:88 c.context.setCert(options.cert); - 这是我得到的错误。 ˇ
猜你喜欢
  • 1970-01-01
  • 2016-09-06
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2020-06-05
相关资源
最近更新 更多