1

So I am successful when deploying my React app to production server. I am using Express for deploying. Here is my code.

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)

I would use CertBot for SSL certs and deploy this as HTTPS server. I really don't know how to approach this problem.

Jakob Valik
  • 41
  • 1
  • 6
  • Possible duplicate of [Enabling HTTPS on express.js](https://stackoverflow.com/questions/11744975/enabling-https-on-express-js) – Sven 31415 May 30 '18 at 18:50

1 Answers1

1

Let's say for example the CertBot generated the SSL certificate and SSL certificate key to the following locations:

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

After you have generated your certificate and key, you'll need to utilize the https module directly.

You'll first need to export your Express app:

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

Then simply import your Express app and hook it up with the https module:

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);

Note this essentially what ./bin/www does for you except I've adapted it to use https instead of http module.

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

Ivalo Pajumets
  • 437
  • 6
  • 11
Cisco
  • 20,972
  • 5
  • 38
  • 60