Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Created June 25, 2019 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschlinkert/ca697ad2a8c57c92ad391026a943657f to your computer and use it in GitHub Desktop.
Save jonschlinkert/ca697ad2a8c57c92ad391026a943657f to your computer and use it in GitHub Desktop.
Simple, cross-platform way to open the browser upon starting an app.
const cp = require('child_process');
const Koa = require('koa');
const app = new Koa();
const cmds = { linux: 'xdg-open', win32: 'start chrome', darwin: 'open' };
const open = cmds[process.platform];
const PORT = 3000;
// response
app.use(ctx => {
ctx.body = 'Hello Koa';
});
cp.execSync(`${open} http://localhost:${PORT}/`);
app.listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment