Skip to content

Commit

Permalink
Merge pull request #768 from roblg/separate-host-and-bind
Browse files Browse the repository at this point in the history
Allows separating public host name from server IP address binding
  • Loading branch information
roblg authored Nov 23, 2016
2 parents 2a761c7 + c898cc7 commit ca859de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
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
6 changes: 5 additions & 1 deletion packages/react-server-cli/src/parseCliArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export default (args = process.argv) => {
type: "number",
})
.option("host", {
describe: "Hostname to start listening for react-server",
describe: "The public host name combined with js-port used to construct URLs for static content. Ignored when js-url is set.",
type: "string",
})
.option("js-port", {
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",
})
.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

0 comments on commit ca859de

Please sign in to comment.