Coding fun – listening yet?

We have a web-hook configured, NGINX is up and reverse-proxying the data, now we work on the actual NodeJS script that’s listening for data, you know the actual work.

As usual, I’m not going into specifics on how to get a NodeJS environment up, there are far too many tutorials that are good at this already.  This post will detail the script specifics that I used to get things running.

That out of the way, assuming you have an environment running, we need to figure out a way to get NodeJS to listen for that incoming web-hook and process it.  NodeJS has the NPM system of module installation, which has so many different modules to use we won’t be hard pressed to find something.

Enter the module Express, we need to install it.  While we’re at it we’ll install another module called body-parser, more on that later.

npm install express --save
npm install body-parser --save

This will install the module, and add it to the package.json file for future install (in case you want to get this running in Docker, hint I will be doing this at the end).

Now we can start our barebones script to listen for those incoming web-hooks

const express = require('express')
const bodyparser = require('body-parser')
var app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.all('/spark', (req, res) => {
  res.send();
  //Parse all the spark/imgur stuff in here...
});

app.listen(90);

I’ll go through this in detail now.

const express = require('express')
const bodyparser = require('body-parser')

Remember those modules we installed?  Here we’re including them, essentially giving us more functionality.  I do this with constants (things that cannot be changed).  This helps me with readability, as I know most constants are modules or hard-coded values that I need later.

var app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));

Ever hear Object Oriented programming?  We’re not doing that here, but we are borrowing some of it’s concepts.  The line var app = express(); is doing just that, we’ve created a variable (var) named it app (warning, I’m not very creative), that is an instantiation of the express object.  This is a really fancy way of saying we are creating a copy of the express module, and getting all the functions, constants, and variables it brings to the table..

There’s more nuances to Object Oriented programming instantiation, but it’s far too much to explain here.  Regardless, this is doing most of the work for us, getting the app ready to listen for those incoming web-hooks.

app.all(..) => {

}

I will go into much more detail on this in my next post.  The app object has a bunch of functions to setup and listen for web requests(like apache, or nginx the webserver).  It can handle GET calls, POST calls, PUT calls, everything you’d ever find in a modern REST API.  You didn’t know you were creating an API?  Surprise, you are (loosely).

app.listen(90);

And that’s all she wrote, this script once executed will listen on TCP port 90, and run until it crashes or we kill it.

Easy?  I think so.  Sure there are HUGE chunks of functionality missing, but at it’s core the act of getting a script to listen for incoming requests is done.

Next time, we fill out some of the meat of this thing.  I’ll explain what sort of data Spark is sending when messages come in (and how you can deduce that for yourself).  We’ll handle that data and grab the contents of a message.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s