Create a simple callback receiver for testing

This simple RESTful callback receiver accepts POST requests from ThingSpace, identifies the callback type, and displays the body via console.log(). You can use it as a development and test tool to quickly see real callback requests from ThingSpace.

NOTE: The computer running this Node.js server must have an external URL. ThingSpace cannot send callback messages to a system that does not have a URL.

Requirements

You must have Node.js installed on the computer, and the Node Package Manager (npm) must be up to date.

This project uses the Express web application framework to provide the HTTP functions, and the body-parser package to process POST requests and JSON.

Instructions

1. Create a new directory to hold the project (feel free to use a different name), and then cd into the new directory:

> mkdir CallbackListener
> cd CallbackListener

2. Create a package.json file and copy in this content:

{
  "name": "fota-callback-listener",
  "version": "1.0.0",
  "description": "A simple Node.js app to receive FOTA callbacks",
  "author": "Verizon",
  "license": "FSFUL"
}

3. Install the Express web application framework :

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

4. Create the cblistener.js file and include this code:

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
 
app.post("/", function(req, res) {
  var size = Object.keys(req.body).length;
  if(req.body.type == "firmwareList") {
    console.log("\nNew Firmware Available:");
  } else if(req.body.type == "upgradeScheduled") {
    console.log("\nUpgrade Scheduled");
  } else if(req.body.type == "upgradeProgress") {
    console.log("\nUpgrade Started");
  } else if(req.body.type == "upgradeFinished") {
    console.log("\nUpgrade Finished");
  } else {
    console.log("\nUnrecognized callback message");
  };
  console.log(req.body);
  return res.end();
  //return res.send(req.body);
});
 
var server = app.listen(3000, function () {
    console.log("Listening on port %s...", server.address().port);
});

You can use a different port number if 3000 doesn’t work in your situation.

5. Install dependencies for the Express and body parser packages.

> npm install

6. Start the callback listener:

> node cblistener.js

7. Register the callback listener with the POST /callbacks API request using the URL for your callback listener:

{
  "name": "Fota",
  "url": "http://your.domain.com:3000"
}

Now you can view the different types of callback requests on the console.