Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mscdex/httpolyglot
Serve http and https connections over the same port with node.js
https://github.com/mscdex/httpolyglot
Last synced: 2 days ago
JSON representation
Serve http and https connections over the same port with node.js
- Host: GitHub
- URL: https://github.com/mscdex/httpolyglot
- Owner: mscdex
- License: mit
- Created: 2014-07-16T02:32:13.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-05-15T13:59:04.000Z (over 5 years ago)
- Last Synced: 2024-05-14T11:21:30.093Z (8 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 179
- Watchers: 10
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Description
===========A module for serving http and https connections over the same port.
Requirements
============* [node.js](http://nodejs.org/) -- v0.10.0 or newer
Install
============npm install httpolyglot
Examples
========* Simple usage:
```javascript
var httpolyglot = require('httpolyglot');
var fs = require('fs');httpolyglot.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}, function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
```* Simple redirect of all http connections to https:
```javascript
var httpolyglot = require('httpolyglot');
var fs = require('fs');httpolyglot.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}, function(req, res) {
if (!req.socket.encrypted) {
res.writeHead(301, { 'Location': 'https://localhost:9000' });
return res.end();
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome, HTTPS user!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
```API
===Exports
-------* **Server** - A class similar to https.Server (except instances have `setTimeout()` from http.Server).
* **createServer**(< _object_ >tlsConfig[, < _function_ >requestListener]) - _Server_ - Creates and returns a new Server instance.
How it Works
============TLS and HTTP connections are easy to distinguish based on the first byte sent by clients trying to connect. See [this comment](https://github.com/mscdex/httpolyglot/issues/3#issuecomment-173680155) for more information.