Skip to content

Commit b664431

Browse files
committed
Adds an option to start HTTPS server rather than HTTP one
1 parent 2d7cfd6 commit b664431

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Topcoder React Utils Changelog
22

33
### v0.8.1
4+
- Adds `https` option to the [standard server factory](docs/server.md),
5+
which makes it easy to run the app on HTTPS instead of HTTP.
46
- Adds `workbox` option to the Wepback configuration factory for app
57
([**`config/webpack/app-base`**](docs/webpack-config.md#configuration-details)).
68

docs/server.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ props:
4747
- **`Application`** — *Component* — Optional. The root ReactJS
4848
component of the app. If provided, server will perform server-side rendering,
4949
and it will inject the rendered markup into the HTML template it serves.
50-
- **`devMode`** — *Boolean* — Optional. Specifies, whether the
51-
server should be launched in the development mode.
52-
- **`favicon`** — *String* — Optional. Path to the favicon to be
53-
served by the server.
54-
- **`logger`** — *Object* — Optional. The logger to be used for
55-
logging. Defaults to `console`, otherwise it is expected to provide the same
56-
interface. Note that `console` is not efficient for production use, because
57-
it is not async in NodeJS.
5850
- **`beforeRender`** — *Function(req, config)* — Optional. The hook to be
5951
executed right before the generation of HTML template of the page.
6052

@@ -72,6 +64,24 @@ props:
7264
injected into the page.
7365
- **`store`** — *Object* — Redux store which state will be
7466
injected into HTML template as the initial state of the app.
67+
68+
- **`devMode`** — *Boolean* — Optional. Specifies, whether the
69+
server should be launched in the development mode.
70+
- **`favicon`** — *String* — Optional. Path to the favicon to be
71+
served by the server.
72+
- **`https`** – *Object* – Optional. Should be an object with two
73+
string fields:
74+
- **`cert`** – *String* – SSL sertificate;
75+
- **`key`** – *String* – SSL key.
76+
77+
For this to work on *localhost* you'll have to create and properly install
78+
a self-signed SSL certificate on your system. Instructions in this article
79+
should help: [How to get HTTPS working on your local development environment in 5 minutes](https://medium.freecodecamp.org/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec)
80+
81+
- **`logger`** — *Object* — Optional. The logger to be used for
82+
logging. Defaults to `console`, otherwise it is expected to provide the same
83+
interface. Note that `console` is not efficient for production use, because
84+
it is not async in NodeJS.
7585
- **`onExpressJsSetup`** — *Function* — Custom setup of ExpressJS
7686
server. Express server instance will be passed in as the only argument to this
7787
function.

src/server/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import _ from 'lodash';
66
import http from 'http';
7+
import https from 'https';
78

89
/* Polyfill required by ReactJS. */
910
import 'raf/polyfill';
@@ -34,6 +35,11 @@ function normalizePort(value) {
3435
* - logger {Object} - Optional. The logger to use. By default, the console
3536
* is used (which is not a good decision performancewise, but it will be
3637
* changed soon);
38+
* - https {Object} - Optional. If given, HTTPS server will be started with
39+
* the specified settings. HTTP server that hanles HTTP > HTTPS redirection
40+
* will be also started at the specified port. Expected fields:
41+
* - cert {String} - SSL Certificate;
42+
* - key {String} - SSL key;
3743
* - beforeRender {Function} - The hook into server-side rendering. It will get
3844
* incoming request as the argument and it should return a promise that will
3945
* resolve to the object with the following fields all optional:
@@ -49,7 +55,7 @@ function normalizePort(value) {
4955
* default to 3000 otherwise.
5056
* @return {Promise} Resolves to the result object has two fields:
5157
* - express {Object} - ExpressJS server;
52-
* - http {Object} - NodeJS HTTP server.
58+
* - httpServer {Object} - NodeJS HTTP(S) server.
5359
*/
5460
export default async function launch(webpackConfig, options) {
5561
/* Options normalization. */
@@ -61,9 +67,16 @@ export default async function launch(webpackConfig, options) {
6167

6268
/* Creates servers, resolves and sets the port. */
6369
const expressServer = await serverFactory(webpackConfig, ops);
64-
const httpServer = http.createServer(expressServer);
6570

66-
/* Sets error handler for HTTP server. */
71+
let httpServer;
72+
if (ops.https) {
73+
httpServer = https.createServer({
74+
cert: ops.https.cert,
75+
key: ops.https.key,
76+
}, expressServer);
77+
} else httpServer = http.createServer(expressServer);
78+
79+
/* Sets error handler for HTTP(S) server. */
6780
httpServer.on('error', (error) => {
6881
if (error.syscall !== 'listen') throw error;
6982
const bind = _.isString(ops.port) ? `Pipe ${ops.port}` : `Port ${ops.port}`;
@@ -83,7 +96,7 @@ export default async function launch(webpackConfig, options) {
8396
}
8497
});
8598

86-
/* Listening event handler for HTTP server. */
99+
/* Listening event handler for HTTP(S) server. */
87100
httpServer.on('listening', () => {
88101
const addr = httpServer.address();
89102
const bind = _.isString(addr) ? `pipe ${addr}` : `port ${addr.port}`;

0 commit comments

Comments
 (0)