Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows separating public host name from server IP address binding #768

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions packages/react-server-cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function start(options){

const {
port,
host,
bindIp,
jsPort,
hot,
jsUrl,
Expand All @@ -47,14 +47,14 @@ export default function start(options){

logger.notice("Starting servers...")

const jsServer = startJsServer(compiler, jsPort, host, longTermCaching, httpsOptions);
const htmlServerPromise = serverRoutes.then(serverRoutesFile => startHtmlServer(serverRoutesFile, port, host, httpsOptions, customMiddlewarePath));
const jsServer = startJsServer(compiler, jsPort, bindIp, longTermCaching, httpsOptions);
const htmlServerPromise = serverRoutes.then(serverRoutesFile => startHtmlServer(serverRoutesFile, port, bindIp, httpsOptions, customMiddlewarePath));

return {
stop: () => Promise.all([jsServer.stop(), htmlServerPromise.then(server => server.stop())]),
started: Promise.all([jsServer.started, htmlServerPromise.then(server => server.started)])
.catch(e => {logger.error(e); throw e})
.then(() => logger.notice(`Ready for requests on host ${host}:${port}.`)),
.then(() => logger.notice(`Ready for requests on ${bindIp}:${port}.`)),
};
}

Expand All @@ -65,7 +65,7 @@ export default function start(options){
// given the server routes file and a port, start a react-server HTML server at
// http://host:port/. returns an object with two properties, started and stop;
// see the default function doc for explanation.
const startHtmlServer = (serverRoutes, port, host, httpsOptions, customMiddlewarePath) => {
const startHtmlServer = (serverRoutes, port, bindIp, httpsOptions, customMiddlewarePath) => {
const server = express();
const httpServer = httpsOptions ? https.createServer(httpsOptions, server) : http.createServer(server);
let middlewareSetup = (server, rsMiddleware) => {
Expand Down Expand Up @@ -104,12 +104,12 @@ const startHtmlServer = (serverRoutes, port, host, httpsOptions, customMiddlewar
console.error(e);
reject(e);
});
httpServer.listen(port, host, (e) => {
httpServer.listen(port, bindIp, (e) => {
if (e) {
reject(e);
return;
}
logger.info(`Started HTML server over ${httpsOptions ? "HTTPS" : "HTTP"} on host ${host}:${port}`);
logger.info(`Started HTML server over ${httpsOptions ? "HTTPS" : "HTTP"} on ${bindIp}:${port}`);
resolve();
});
}),
Expand All @@ -120,7 +120,7 @@ const startHtmlServer = (serverRoutes, port, host, httpsOptions, customMiddlewar
// files and start up a web server at http://host:port/ that serves the
// static compiled JavaScript. returns an object with two properties, started and stop;
// see the default function doc for explanation.
const startStaticJsServer = (compiler, port, host, longTermCaching, httpsOptions) => {
const startStaticJsServer = (compiler, port, bindIp, longTermCaching, httpsOptions) => {
const server = express();
const httpServer = httpsOptions ? https.createServer(httpsOptions, server) : http.createServer(server);
return {
Expand All @@ -145,13 +145,13 @@ const startStaticJsServer = (compiler, port, host, longTermCaching, httpsOptions
console.error(e);
reject(e)
});
httpServer.listen(port, host, (e) => {
httpServer.listen(port, bindIp, (e) => {
if (e) {
reject(e);
return;
}

logger.info(`Started static JavaScript server over ${httpsOptions ? "HTTPS" : "HTTP"} on host ${host}:${port}`);
logger.info(`Started static JavaScript server over ${httpsOptions ? "HTTPS" : "HTTP"} on ${bindIp}:${port}`);
resolve();
});
});
Expand All @@ -163,7 +163,7 @@ const startStaticJsServer = (compiler, port, host, longTermCaching, httpsOptions
// for hot reloading at http://localhost:port/. note that the webpack compiler
// must have been configured correctly for hot reloading. returns an object with
// two properties, started and stop; see the default function doc for explanation.
const startHotLoadJsServer = (compiler, port, host, longTermCaching, httpsOptions) => {
const startHotLoadJsServer = (compiler, port, bindIp, longTermCaching, httpsOptions) => {
logger.info("Starting hot reload JavaScript server...");
const compiledPromise = new Promise((resolve) => compiler.plugin("done", () => resolve()));
const jsServer = new WebpackDevServer(compiler, {
Expand All @@ -176,7 +176,7 @@ const startHotLoadJsServer = (compiler, port, host, longTermCaching, httpsOption
ca: httpsOptions ? httpsOptions.ca : undefined,
});
const serverStartedPromise = new Promise((resolve, reject) => {
jsServer.listen(port, host, (e) => {
jsServer.listen(port, bindIp, (e) => {
if (e) {
reject(e);
return;
Expand All @@ -187,7 +187,7 @@ const startHotLoadJsServer = (compiler, port, host, longTermCaching, httpsOption
return {
stop: serverToStopPromise(jsServer),
started: Promise.all([compiledPromise, serverStartedPromise])
.then(() => logger.info(`Started hot reload JavaScript server over ${httpsOptions ? "HTTPS" : "HTTP"} on host ${host}:${port}`)),
.then(() => logger.info(`Started hot reload JavaScript server over ${httpsOptions ? "HTTPS" : "HTTP"} on ${bindIp}:${port}`)),
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/react-server-cli/src/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
host: "localhost",
port:3000,
jsPort: 3001,
bindIp: "0.0.0.0",
hot: true,
minify: false,
compileOnly: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-server-cli/src/parseCliArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default (args = process.argv) => {
describe: "Port to start listening for react-server's JavaScript. Default is 3001.",
type: "number",
})
.option("bind-ip", {
describe: "IP address to bind to. Default: 0.0.0.0 (any)",
type: "string",
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we call it bindIp internally to disambiguate, maybe the CLI option should be --bind-ip, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

.option("https", {
describe: "If true, the server will start up using https with a self-signed certificate. Note that browsers do not trust self-signed certificates by default, so you will have to click through some warning screens. This is a quick and dirty way to test HTTPS, but it has some limitations and should never be used in production. Requires OpenSSL to be installed. Default is false.",
default: undefined,
Expand Down