ember-fastboot / ember-fastboot/fastboot-app-server

Common use case: force HTTPS via beforeMiddleware

Open
#36 8 comments 3 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
140
Forks
70
PR merge metrics
No merged PRs in 30d

Description

Volunteering some example code, as a potential candidate for README. This uses the relatively new `beforeMiddleware` hook to force http traffic to https, with a provision for the EBS health check feature (which seems to come over HTTP unless HTTP is totally disabled for the env).

```js

const enforceHTTPS = (req, res, next) => {
// Header indicates edge server received request over HTTPS
if (req.headers['x-forwarded-proto'] === 'https'){
return next();
} else {
// Did not come over HTTPS. Fix that!
return res.redirect(301, join(`https://${req.hostname}${req.url}`));
}
};

let server = new FastBootAppServer({
...
beforeMiddleware(app) {
app.use((req, res, next) => {
if (process.env.DISABLE_FORCE_HTTPS || // Ability to disable force HTTPS via env
req.headers['user-agent'].indexOf('HealthChecker') >= 0) { // EBS health over HTTP
return next(); // Proceed as planned (http or https -- whatever was asked for)
} else {
return enforceHTTPS(req, res, next); // Middleware to force all other HTTP --> HTTPS
}
});
}
});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.