-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0cf8d01
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
root = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.js] | ||
trim_trailing_whitespace = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/node_modules | ||
package-lock.json | ||
|
||
# IDE | ||
.idea | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) mahovich | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# koa-force-https · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/mahovich/koa-force-https/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/koa-force-https.svg?style=flat)](https://www.npmjs.com/package/koa-force-https) | ||
|
||
[Koa.js](https://koajs.com/) middleware to force HTTPS connection on any incoming requests. In case of a non-encrypted HTTP request, `koa-force-https` automatically redirects to an HTTPS address. | ||
|
||
## Requirements | ||
- Koa@2+ | ||
- Node@6+ | ||
|
||
## Installation | ||
``` | ||
$ npm install koa-force-https --save | ||
``` | ||
|
||
## Options | ||
|
||
| Name | Type | Default | Description | | ||
|------------------|---------|-------------|--------------------------------------------------------------------------| | ||
| `port` | Integer | `443` | Port of HTTPS server (port `:443` is automatically removed from the URL) | | ||
| `hostname` | String | `same host` | Hostname for redirect | | ||
| `httpStatusCode` | Integer | `301` | HTTP status code for redirect | | ||
|
||
## Usage | ||
``` | ||
const Koa = require('koa'); | ||
const forceHTTPS = require('koa-force-https'); | ||
const app = new Koa(); | ||
app.use(forceHTTPS()); | ||
``` | ||
|
||
## Examples | ||
|
||
### Redirect requests from `http` to `https` | ||
``` | ||
const fs = require('fs'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
const Koa = require('koa'); | ||
const forceHTTPS = require('koa-force-https'); | ||
const options = { | ||
key: fs.readFileSync('ssl-certificate.key'), | ||
cert: fs.readFileSync('ssl-certificate.crt') | ||
} | ||
const app = new Koa(); | ||
app.use(forceHTTPS()); | ||
app.use((ctx) => { | ||
ctx.body = 'Hello, world! This is an HTTPS connection.'; | ||
}); | ||
// Runs 2 servers for the application. Requests from the HTTP server will be redirected to the HTTPS server. | ||
http.createServer(app.callback()).listen(80); | ||
https.createServer(options, app.callback()).listen(443); | ||
``` | ||
|
||
**Some results requests for this example** | ||
|
||
| Request URL | Status Code | Location | | ||
|------------------------------|-------------|-------------------------------| | ||
| http://example.com | `301` | https://example.com | | ||
| http://www.example.com | `301` | https://www.example.com | | ||
| http://www.example.com/news | `301` | https://www.example.com/news | | ||
| http://www.example.com/?id=1 | `301` | https://www.example.com/?id=1 | | ||
| http://example.com/news?id=1 | `301` | https://example.com/news?id=1 | | ||
| https://example.com | `200` | *no change* | | ||
| https://www.example.com | `200` | *no change* | | ||
|
||
### Redirect requests from `http` to `https` (using HTTP/2 protocol) to hostname `example.com` using the HTTP status code `307` ("307 Temporary Redirect") | ||
``` | ||
const fs = require('fs'); | ||
const http = require('http'); | ||
const http2 = require('http2'); | ||
const Koa = require('koa'); | ||
const forceHTTPS = require('koa-force-https'); | ||
const options = { | ||
key: fs.readFileSync('ssl-certificate.key'), | ||
cert: fs.readFileSync('ssl-certificate.crt') | ||
} | ||
const app = new Koa(); | ||
app.use(forceHTTPS(undefined, 'example.com', 307)); | ||
app.use((ctx) => { | ||
ctx.body = 'Hello, world! This is an HTTPS connection using HTTP/2 protocol.'; | ||
}); | ||
http.createServer(app.callback()).listen(80); | ||
http2.createSecureServer(options, app.callback()).listen(443); | ||
``` | ||
|
||
**Some results requests for this example** | ||
|
||
| Request URL | Status Code | Location | | ||
|------------------------------|-------------|-------------------------------| | ||
| http://example.com | `307` | https://example.com | | ||
| http://www.example.com | `307` | https://example.com | | ||
| http://www.example.com/news | `307` | https://example.com/news | | ||
| http://www.example.com/?id=1 | `307` | https://example.com/?id=1 | | ||
| http://example.com/news?id=1 | `307` | https://example.com/news?id=1 | | ||
| https://example.com | `200` | *no change* | | ||
| https://www.example.com | `200` | *no change* | | ||
|
||
## License | ||
koa-force-https is [MIT licensed](https://github.com/mahovich/koa-force-https/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const url = require('url'); | ||
|
||
/** | ||
* Force HTTPS connection on any incoming requests | ||
* | ||
* @param {Integer} port | ||
* @param {String} hostname | ||
* @param {Integer} httpStatusCode | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
|
||
module.exports = (port = 443, hostname, httpStatusCode = 301) => (ctx, next) => { | ||
if (ctx.secure) return next(); | ||
|
||
const httpsPort = (port === 443) ? '' : `:${port}`; | ||
const httpsHost = hostname || url.parse(`http://${ctx.request.header.host}`).hostname; | ||
|
||
ctx.response.status = httpStatusCode; | ||
ctx.response.redirect(`https://${httpsHost}${httpsPort}${ctx.request.url}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "koa-force-https", | ||
"version": "1.0.0", | ||
"description": "Koa.js middleware to force HTTPS connection on any incoming requests", | ||
"main": "index.js", | ||
"homepage": "https://github.com/mahovich/koa-force-https", | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:mahovich/koa-force-https.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/mahovich/koa-force-https/issues" | ||
}, | ||
"devDependencies": { | ||
"koa": "^2.0.0" | ||
}, | ||
"keywords": [ | ||
"koa", | ||
"koa.js", | ||
"middleware", | ||
"https", | ||
"ssl", | ||
"http2", | ||
"redirect", | ||
"force", | ||
"node", | ||
"node.js" | ||
], | ||
"author": "mahovich", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">6.0.0" | ||
} | ||
} |