Ping a server from a grunt task

I was working on a small project that sets up a proxy using grunt, and then starts a node server. Problem is that if the proxy wasn’t up and running, grunt would still start the server and any call to the proxy would make the grunt server crash with some unrelated error.

I decided to add a simple task that would ping the proxy server, and simply fail with an explicit error if it wasn’t responding.

I found lots of different node modules but most of them were simple wrappers to the ping command line: that wasn’t very useful, especially since I wanted something portable.

Then I found tcp-ping which attempts to contact any server, on any port at tcp-level which is not only much faster than traditionnal ping but allows to test any service and not the whole connection.

After installing the module like any other node module:

In a few minutes I came up with this little task:

As you can see this is quite easy. To be sure your task prevents your server from running if the proxy isn’t there you can simply call the task before the connect task:

If your server isn’t running, you will end up with the following explicit error:


Warpdesign.fr