├── .editorconfig ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | charset = utf-8 9 | indent_style = space 10 | indent_size = 2 11 | 12 | # We recommend you to keep these unchanged 13 | end_of_line = lf 14 | charset = utf-8 15 | trim_trailing_whitespace = true 16 | insert_final_newline = true 17 | 18 | [*.md] 19 | trim_trailing_whitespace = false 20 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Vitaly Domnikov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # koa-force-ssl 2 | 3 | ![KoaJs Slack](https://img.shields.io/badge/Koa.Js-Slack%20Channel-Slack.svg?longCache=true&style=for-the-badge) 4 | 5 | [Koa.js](http://koajs.com/) middleware for force SSL 6 | 7 | *This middleware is deprecated, please use [koa-sslify](https://github.com/turboMaCk/koa-sslify)* 8 | 9 | 10 | 11 | ## Install 12 | ``` 13 | $ npm install koa-force-ssl 14 | ``` 15 | 16 | ## API 17 | `forceSSL(port, hostname, temporary);` 18 | 19 | * port - SSL port (default value: 443) 20 | * hostname - host name for redirect (by default will redirect to same host) 21 | * temporary - use "302 Temporary Redirect" (by default will use "301 Permanent Redirect") 22 | 23 | ## Example 24 | ``` 25 | var koa = require('koa'); 26 | var http = require('http'); 27 | var https = require('https'); 28 | var fs = require('fs'); 29 | var forceSSL = require('koa-force-ssl'); 30 | 31 | var app = koa(); 32 | 33 | // Force SSL on all page 34 | app.use(forceSSL()); 35 | 36 | // index page 37 | app.use(function * (next) { 38 | this.body = "hello world from " + this.request.url; 39 | }); 40 | 41 | // SSL options 42 | var options = { 43 | key: fs.readFileSync('server.key'), 44 | cert: fs.readFileSync('server.crt') 45 | } 46 | 47 | // start the server 48 | http.createServer(app.callback()).listen(80); 49 | https.createServer(options, app.callback()).listen(443); 50 | ``` 51 | 52 | ## License 53 | MIT 54 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var url = require('url'); 2 | 3 | /** 4 | * Force SSL. 5 | * 6 | * @param {Integer} port 7 | * @param {String} hostname 8 | * @param {Boolean} temporary 9 | * @return {Function} 10 | * @api public 11 | */ 12 | 13 | module.exports = function forceSSL(port, hostname, temporary) { 14 | return function* forceSSL(next) { 15 | if (this.secure) { 16 | return yield next; 17 | } 18 | var httpsPort = port || 443; 19 | var urlObject = url.parse('http://' + this.request.header.host); 20 | var httpsHost = hostname || urlObject.hostname; 21 | if (!temporary) { 22 | this.response.status = 301; 23 | } 24 | this.response.redirect('https://' + httpsHost + ':' + httpsPort + this.request.url); 25 | }; 26 | }; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-force-ssl", 3 | "version": "0.0.5", 4 | "description": "Koa.js middleware for force SSL", 5 | "main": "index.js", 6 | "homepage": "http://github.com/dotcypress/koa-force-ssl", 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/dotcypress/koa-force-ssl.git" 10 | }, 11 | "bugs": { 12 | "url": "http://github.com/dotcypress/koa-force-ssl/issues" 13 | }, 14 | "devDependencies": { 15 | "koa": "^0.6.0" 16 | }, 17 | "keywords": [ 18 | "koa", 19 | "koa.js", 20 | "middleware", 21 | "https", 22 | "ssl" 23 | ], 24 | "author": "Vitaly Domnikov", 25 | "license": "MIT", 26 | "engines": { 27 | "node": "^0.11.12" 28 | } 29 | } 30 | --------------------------------------------------------------------------------