1

I have a website built with reactjs front-end and express server as back-end, communicating over http. Now I need to switch to https. Where do I need to make changes?

My understanding is that I mostly need to change the express server and in the front-end to only switch urls to https://url. I implemented standard express https server configuration.

Bellow is my express server code:

const express = require('express')
const fs = require('fs')
const https = require('https')
const cors = require('cors')
const bodyParser = require('body-parser')
const passport = require('passport')
const localSignupStrategy = require('./passport/local-signup')
const localLoginStrategy = require('./passport/local-login')
const authRoutes = require('./routes/auth')
const productsRoutes = require('./routes/products')

const path = require('path')
// db
let env = process.env.NODE_ENV || 'development'
let settings = require('./config/settings')[env]

const app = express()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(passport.initialize())
app.use(cors())
// db
require('./config/database')(settings)

passport.use('local-signup', localSignupStrategy)
passport.use('local-login', localLoginStrategy)

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    next()
})

app.use(express.static(path.join(__dirname, '../public')))

// routes
app.use('/auth', authRoutes)
app.use('/products', productsRoutes)

let options = {
    key: fs.readFileSync('privatekey.pem'),
    cert: fs.readFileSync('certificate.pem')
}

https.createServer(options, app).listen(settings.httpsPort, () => {
    console.log(`Server running on port ${settings.httpsPort}...`)
})

When testing locally I get this error:

"Failed to load resource: net::ERR_CONNECTION_REFUSED"

when making my server calls and I'm lost on what that's telling me exactly. I am not sure if it is the certificates, server config or the react client and I'm generally lost on how to think about solving the problem.

Any guidance would be appreciated.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Possible duplicate of [Enabling HTTPS on express.js](https://stackoverflow.com/questions/11744975/enabling-https-on-express-js) – rckrd Dec 29 '18 at 12:02
  • I think I'm using the express config suggested in the accepted answer. It was one of the places I looked for a solution. – Dimitar Valchev Dec 29 '18 at 22:22
  • is the certificate self-signed? – gdanov Jan 07 '19 at 15:18
  • I tried with a self-signed certificate and I tried with a certificate from my provider. The result is the same error message and I'm unable to get close to what exactly is wrong. – Dimitar Valchev Jan 07 '19 at 17:43

0 Answers0