Skip to content

Commit c22c96b

Browse files
dceddiagaearon
authored andcommitted
Look for HTTPS environment variable (#430)
With the HTTPS env var set 'true', the dev server will serve over HTTPS.
1 parent f1c9584 commit c22c96b

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-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
// By default WebpackDevServer also serves files from the current directory.
225225
// This might be useful in legacy apps. However we already encourage people
@@ -254,7 +254,9 @@ function runDevServer(port) {
254254
// https://github.com/facebookincubator/create-react-app/issues/293
255255
watchOptions: {
256256
ignored: /node_modules/
257-
}
257+
},
258+
// Enable HTTPS if the HTTPS environment variable is set to 'true'
259+
https: protocol === "https" ? true : false
258260
});
259261

260262
// Our custom middleware proxies requests to /index.html or a remote API.
@@ -269,13 +271,14 @@ function runDevServer(port) {
269271
clearConsole();
270272
console.log(chalk.cyan('Starting the development server...'));
271273
console.log();
272-
openBrowser(port);
274+
openBrowser(port, protocol);
273275
});
274276
}
275277

276278
function run(port) {
277-
setupCompiler(port);
278-
runDevServer(port);
279+
var protocol = process.env.HTTPS === 'true' ? "https" : "http";
280+
setupCompiler(port, protocol);
281+
runDevServer(port, protocol);
279282
}
280283

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

template/README.md

+25
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 `<link>` and `<meta>` Tags](#adding-link-and-meta-tags)
2829
- [Referring to Static Assets from `<link href>`](#referring-to-static-assets-from-link-href)
2930
- [Generating Dynamic `<meta>` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
@@ -528,6 +529,30 @@ If the `proxy` option is **not** flexible enough for you, alternatively you can:
528529
* Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
529530
* Use [environment variables](#adding-custom-environment-variables) to inject the right server host and port into your app.
530531
532+
## Using HTTPS in Development
533+
534+
>Note: this feature is available with `[email protected]` and higher.
535+
536+
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.
537+
538+
To do this, set the `HTTPS` environment variable to `true`, then start the dev server as usual with `npm start`:
539+
540+
#### Windows (cmd.exe)
541+
542+
```cmd
543+
set HTTPS=true&&npm start
544+
```
545+
546+
(Note: the lack of whitespace is intentional.)
547+
548+
#### Linux, OS X (Bash)
549+
550+
```bash
551+
HTTPS=true npm start
552+
```
553+
554+
Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.
555+
531556
## Adding `<link>` and `<meta>` Tags
532557
533558
You can edit the generated `index.html` and add any tags you’d like to it.

0 commit comments

Comments
 (0)