Skip to content

Commit

Permalink
Replaced deprecated function url.parse()
Browse files Browse the repository at this point in the history
About deprecated function: https://nodejs.org/api/deprecations.html#deprecations_dep0116_legacy_url_api
Updated engine node to 6.13 due to the use of a new function
Update README.md
  • Loading branch information
mahovich committed Mar 9, 2019
1 parent 9af89d3 commit bb61aad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/node_modules
package-lock.json
yarn.lock

# IDE
.idea
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Requirements
- Koa@2+
- Node@6+
- Node@6.13+

## Installation
```
Expand All @@ -13,11 +13,11 @@ $ 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 |
| Name | Type | Default | Description |
|------------------|---------|-------------|----------------------------------------------------------------|
| `port` | Integer | | HTTPS port (port `:443` is automatically removed from the URL) |
| `hostname` | String | `same host` | Hostname for redirect |
| `httpStatusCode` | Integer | `301` | HTTP status code for redirect |

## Usage
```
Expand Down Expand Up @@ -62,8 +62,8 @@ https.createServer(options, app.callback()).listen(443);

| Request URL | Status Code | Location |
|--------------------------------|-------------|---------------------------------|
| `http://example.com` | `301` | `https://example.com` |
| `http://www.example.com` | `301` | `https://www.example.com` |
| `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` |
Expand Down Expand Up @@ -99,13 +99,13 @@ http2.createSecureServer(options, app.callback()).listen(443);

| Request URL | Status Code | Location |
|--------------------------------|-------------|---------------------------------|
| `http://example.com` | `307` | `https://example.com` |
| `http://www.example.com` | `307` | `https://example.com` |
| `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 redirect* |
| `https://www.example.com` | `200` | *no redirect* |

## License
koa-force-https is [MIT licensed](https://github.com/mahovich/koa-force-https/blob/master/LICENSE).
`koa-force-https` is [MIT licensed](https://github.com/mahovich/koa-force-https/blob/master/LICENSE).
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const url = require('url');

/**
* Force HTTPS connection on any incoming requests
*
Expand All @@ -10,12 +8,14 @@ const url = require('url');
* @api public
*/

module.exports = (port = 443, hostname, httpStatusCode = 301) => (ctx, next) => {
module.exports = (port, 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;
const urlRedirect = ctx.request.URL;
urlRedirect.protocol = 'https';
if (port) urlRedirect.port = port;
if (hostname) urlRedirect.hostname = hostname;

ctx.response.status = httpStatusCode;
ctx.response.redirect(`https://${httpsHost}${httpsPort}${ctx.request.url}`);
ctx.response.redirect(urlRedirect.href);
};
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-force-https",
"version": "1.0.1",
"version": "2.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",
Expand All @@ -22,13 +22,11 @@
"ssl",
"http2",
"redirect",
"force",
"node",
"node.js"
"force"
],
"author": "mahovich",
"license": "MIT",
"engines": {
"node": ">6.0.0"
"node": ">=6.13.0"
}
}

0 comments on commit bb61aad

Please sign in to comment.