Sundered Peak

through the mind of kyle tolle

Using Node.js to inspect Webhook calls

The Idea

Recently, I needed a simple HTTP server. I’ve done a little fiddling with Node.js lately, so I thought it’d be a good use case. What did I need it for? I needed an endpoint for some Webhook functionality. At this endpoint, I wanted to be able to print out the contents of the POST calls to the webhook URL.

This is what I ended up with:

Installation

First, install Node.js. (My version is 0.10.17)

On a mac:

brew install node

Then, clone the gist above to your machine.

git clone https://gist.github.com/6530914.git node-http-post-listener

Running

Next, cd into the directory and run the code.

cd node-http-post-listener
node node-http-post-listener.js

The server is set to listen on port 9000 and will return a 200 code when called. This is indicated by the output when the server starts:

Listening to port 9000
Returning status code 200

Testing

Finally, you can check to make sure it’s listening properly with a POST message.

From another terminal window:

curl -X POST localhost:9000 -H 'Content-Type: application/json' -d '{"payload":"test"}'

Everything is working right if you see the following output from the server:

Received body data:
{"payload":"test"}

Customizing

It’s easy to change app.listen to listen on another port.

If you want to respond with an error code to test the error handling ability of the calling Webhook code, you can change statusCode to be 500, 404, or whatever you need.

Feedback

I’d love to get your feedback on if you’ve used something like this before. If you’d like to make a change to the code, feel free to as well.