Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a test suite which runs in a browser (using QUnit, though that's unimportant). The test suite requires a local server running on a particular port. Running the tests currently involves a few steps:

  1. node test/server
  2. switch to browser of choice
  3. open localhost:3000

I'd like to have a single command which starts the server then runs open localhost:3000. Ideally, the node process would remain in the foreground so it could be killed with ^C. I don't have a good understanding of subshells, so I'm not sure whether this is possible.

share|improve this question
what OS? I think the answer may vary based on it – Journeyman Geek Dec 3 '12 at 6:45
I'd like to support multiple platforms. I've already written a tiny program which forwards arguments to whichever "open" command is available: (commands beyond open and xdg-open could easily be added). The part I'm stuck on is doing something like node test/server && open localhost:3000 (but which actually works). – davidchambers Dec 3 '12 at 6:50
up vote 1 down vote accepted

If I understand correctly, you want to:

  • start a node.js service
  • start a browser
  • visit the service in the browser

First start the browser after a given delay. Next, start the service and let it run in the foreground. The double ampersand operator forces the "next" command to wait for the "first" command to exit with a code of 0, while the single ampersand means "start in background". We can use this to buy the service some time to warm up before the browser tries to access it.

sleep $awhile && firefox http://localhost:3000/ & 
node netservice.js 

If it helps, you can think of the foo && bar & as an async call that guarantees foo will finish before bar is called.

share|improve this answer
Very helpful answer. I'll try this when I get home. – davidchambers Dec 3 '12 at 23:15
This worked beautifully, and a 0.1 second sleep proved sufficient. – davidchambers Dec 4 '12 at 5:18

The instructions say to open your browser of choice, as in Web browser, namely Firefox, Chrome, ahem... Internet Explorer.

Steps 2 and 3 are not command line at all. Step 3 is paste 'localhost:3000' into the browser's address bar. You can leave node running in the shell, or appen '&' which would start the process and return you to the prompt.

share|improve this answer
Right. I'm trying to automate the manual steps. OS X provides open, and other platforms provide similar commands for opening a resource in the appropriate application. The part I'm stuck on is making something like node test/server && open localhost:3000 work. – davidchambers Dec 3 '12 at 6:45
Ah, I see. I knew I was misunderstanding something. Um, good luck. – Ian Atkin Dec 3 '12 at 7:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .