Skip to content

Commit 61d7af8

Browse files
committed
https redirect
1 parent fe0058d commit 61d7af8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Open [mern-notesapp.herokuapp.com](https://mern-notesapp.herokuapp.com/) or [rea
88

99
## Development
1010

11-
First of all, set up your project by creating .env file with next content:
11+
First of all, set up your project by creating `.env` file with next content:
1212

1313
```env
1414
mongoUri = "<YOUR MONGO URI>"
@@ -57,7 +57,8 @@ module.exports = {
5757
"max-memory-restart": "150MB",
5858
env: {
5959
NODE_ENV: "production",
60-
mongoUri: "<YOUR MONGO URI>",
60+
mongoUri: "<YOUR MONGO URI>" /*replace this*/,
61+
httpsRedirect: false /*true if enable*/,
6162
},
6263
},
6364
],
@@ -69,5 +70,8 @@ module.exports = {
6970
- Create Heroku app
7071
- Connect to GitHub repo
7172
- Add Node.js buildpack in settings
72-
- Add config vars
73+
- Add config vars:
74+
- NODE_ENV: production
75+
- mongoUri: YOUR MONGO URI
76+
- httpsRedirect: true
7377
- Click "Deploy branch" button

app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('dotenv').config()
88
const devMode = process.env.NODE_ENV === "dev"
99
const PORT = process.env.PORT || 5000
1010
const mongoUri = process.env.mongoUri
11+
const httpsRedirect = process.env.httpsRedirect || false
1112

1213
//temporary backend url
1314
const phpBaseUrl = 'http://php-server-notes.std-1033.ist.mospolytech.ru/'
@@ -24,6 +25,7 @@ app.post('/server', function (req, res) {
2425
})
2526

2627
if (!devMode) {
28+
if (httpsRedirect) app.use(httpToHttps)
2729
app.use('/', express.static(path.join(__dirname, 'client', 'build')))
2830
app.get('*', (req, res) => {
2931
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
@@ -61,4 +63,12 @@ function logServerStart(PORT) {
6163
console.log(`${bef} Local${af} http://localhost:${PORT}`)
6264
console.log(`${bef} On Your Network${af} http://${address}:${PORT}`)
6365
})
66+
}
67+
68+
function httpToHttps(req, res, next) {
69+
if (req.header('x-forwarded-proto') !== 'https') {
70+
res.redirect(`https://${req.header('host')}${req.url}`)
71+
} else {
72+
next()
73+
}
6474
}

0 commit comments

Comments
 (0)