Skip to content

Commit 3e52607

Browse files
committed
Look for HTTPS environment variable (#430)
With the HTTPS env var set 'true', the dev server will serve over HTTPS.
1 parent a94b252 commit 3e52607

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

scripts/start.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function clearConsole() {
7474
process.stdout.write('\x1bc');
7575
}
7676

77-
function setupCompiler(port) {
77+
function setupCompiler(port, protocol) {
7878
// "Compiler" is a low-level interface to Webpack.
7979
// It lets us listen to some events and provide our own custom messages.
8080
compiler = webpack(config, handleCompile);
@@ -99,7 +99,7 @@ function setupCompiler(port) {
9999
console.log();
100100
console.log('The app is running at:');
101101
console.log();
102-
console.log(' ' + chalk.cyan('http://localhost:' + port + '/'));
102+
console.log(' ' + chalk.cyan(protocol + '://localhost:' + port + '/'));
103103
console.log();
104104
console.log('Note that the development build is not optimized.');
105105
console.log('To create a production build, use ' + chalk.cyan('npm run build') + '.');
@@ -150,14 +150,14 @@ function setupCompiler(port) {
150150
});
151151
}
152152

153-
function openBrowser(port) {
153+
function openBrowser(port, protocol) {
154154
if (process.platform === 'darwin') {
155155
try {
156156
// Try our best to reuse existing tab
157157
// on OS X Google Chrome with AppleScript
158158
execSync('ps cax | grep "Google Chrome"');
159159
execSync(
160-
'osascript chrome.applescript http://localhost:' + port + '/',
160+
'osascript chrome.applescript ' + protocol + '://localhost:' + port + '/',
161161
{cwd: path.join(__dirname, 'utils'), stdio: 'ignore'}
162162
);
163163
return;
@@ -167,7 +167,7 @@ function openBrowser(port) {
167167
}
168168
// Fallback to opn
169169
// (It will always open new tab)
170-
opn('http://localhost:' + port + '/');
170+
opn(protocol + '://localhost:' + port + '/');
171171
}
172172

173173
function addMiddleware(devServer) {
@@ -219,7 +219,7 @@ function addMiddleware(devServer) {
219219
devServer.use(devServer.middleware);
220220
}
221221

222-
function runDevServer(port) {
222+
function runDevServer(port, protocol) {
223223
var devServer = new WebpackDevServer(compiler, {
224224
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
225225
// for the WebpackDevServer client so it can learn when the files were
@@ -237,7 +237,9 @@ function runDevServer(port) {
237237
// https://github.com/facebookincubator/create-react-app/issues/293
238238
watchOptions: {
239239
ignored: /node_modules/
240-
}
240+
},
241+
// Enable HTTPS if the HTTPS environment variable is set to 'true'
242+
https: protocol === "https" ? true : false
241243
});
242244

243245
// Our custom middleware proxies requests to /index.html or a remote API.
@@ -252,13 +254,14 @@ function runDevServer(port) {
252254
clearConsole();
253255
console.log(chalk.cyan('Starting the development server...'));
254256
console.log();
255-
openBrowser(port);
257+
openBrowser(port, protocol);
256258
});
257259
}
258260

259261
function run(port) {
260-
setupCompiler(port);
261-
runDevServer(port);
262+
var protocol = process.env.HTTPS === 'true' ? "https" : "http";
263+
setupCompiler(port, protocol);
264+
runDevServer(port, protocol);
262265
}
263266

264267
// We attempt to use the default port but if it is busy, we offer the user to

template/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
2424
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
2525
- [Integrating with a Node Backend](#integrating-with-a-node-backend)
2626
- [Proxying API Requests in Development](#proxying-api-requests-in-development)
27+
- [Using HTTPS in Development](#using-https-in-development)
2728
- [Adding `<meta>` Tags](#adding-meta-tags)
2829
- [Running Tests](#running-tests)
2930
- [Filename Conventions](#filename-conventions)
@@ -526,6 +527,28 @@ If the `proxy` option is **not** flexible enough for you, alternatively you can:
526527
* Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
527528
* Use [environment variables](#adding-custom-environment-variables) to inject the right server host and port into your app.
528529
530+
## Using HTTPS in Development
531+
532+
You may require the dev server to serve pages over HTTPS. One particular case where this could be useful is when using [the "proxy" feature](#proxying-api-requests-in-development) to proxy requests to an API server when that API server is itself serving HTTPS.
533+
534+
To do this, set the `HTTPS` environment variable to `true`, then start the dev server as usual with `npm start`:
535+
536+
#### Windows (cmd.exe)
537+
538+
```cmd
539+
set HTTPS=true&&npm start
540+
```
541+
542+
(Note: the lack of whitespace is intentional.)
543+
544+
#### Linux, OS X (Bash)
545+
546+
```bash
547+
HTTPS=true npm start
548+
```
549+
550+
Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.
551+
529552
## Adding `<meta>` Tags
530553
531554
You can edit the generated `index.html` and add any tags you’d like to it. However, since Create React App doesn’t support server rendering, you might be wondering how to make `<meta>` tags dynamic and reflect the current URL.

0 commit comments

Comments
 (0)