My nano-service

Matt
4 min readNov 17, 2020

A simple node test application

Sometimes you just need a small teeny tiny simple application that works to test something out. Something not much larger than a hello world. Before I document my mini app and explain why I, or you might need one, lets first digress a bit… for fun.

In the metrics system a micrometer aka micron is x10^–3, and wikipedia shows us just how tiny that is. A blood cell is 5–10 microns, spider silk is 2–3 microns (nature is awesome btw), and bacteria is 1 micron. That’s tiny !!!

A micro-services can be a small api, a large api or in some cases even begin to be a not-so-small at all service. So what about a hello world type services? To me that is much smaller than a micro-service. So i decided to call it the nano-service.

Nano is 10^-6 in size. To get an idea of the size of a nanometer consider that DNA is 2.5 nanometers !! Check it out its cool.

Let’s get back on track here…

Why do I want a tiny service?

Well in my case i simply want to ensure traffic is flowing and reaching my service. A simple hello world would have sufficed. But since i’m at it why not just print out a few details more than a static message. I will call this app the request-capture. Simply, because it will “capture” (basically print) the request that the app receives.

A little preparation is required first:

Check out my node.js development preparation post.

The DNA (code) of our nano service:

Sorry about the pun, i can’t help. Here is the code:

const express = require('express');
const app = express();
const port = process.argv.slice(2)[0];
const appname = "request-capture";
const version = "v1.0.0"
const url_printer = function (req, res, mypath) {// Let's return the request url with all the smaller parts that comprise this url.var protocol = req.protocol + "://";
var domainname = req.get('host');
var path_with_query = req.originalUrl;
var url = protocol + domainname + path_with_query;
// We want this to be displayed as a nice json output
var output = {
name: appname,
version: version,
matchedpath: mypath,
url: url
}
console.log(JSON.stringify(output) + "\n");
res.send(JSON.stringify(output) + "\n");
};

app.use('/my/exampleA', (req, res) => {
url_printer(req, res, "/my/exampleA")
});
app.use('/my/exampleB', (req, res) => {
url_printer(req, res, "/my/exampleB")
});
app.get('/my/*', (req, res) => {
url_printer(req, res, "my/*")
});
// Allows safe termination with `control c`process.on('SIGINT', () => {
console.log('SIGINT signal received: closing HTTP server')
server.close(() => {
console.log('HTTP server closed')
});
});
process.on('SIGTERM', () => {
console.log('SIGTERM signal received: closing HTTP server')
server.close(() => {console.log('HTTP server closed')
});
});
console.log(appname + " service version " + version + " listening on port " + port);
console.log("To terminate safely simply type `Control C`");
var server = app.listen(port);

TODO: make the code a gist then i can embed it in the blog to display it formatted nicer.

Running it:

The application will take a port input on startup and start the express.js server listening to requests on that port. When a request comes in it will be matched against one of the following app.get or app.use rules. Note: Just think of app.use as also matching requests for all method types: GET, POST,etc.

Based on the path sent to the application it will determine if it matches /my/exampleA, /my/exampleB, or the catch all for anything else /my/* and replies accordingly.

Running the application:

Before we start we need to add the app dependancy:

npm install --save express

To start a node application we use the format:

node <app-entry.js> <arguments>

So for our example:

node request-capture.js 8081

Before you run it, we need to select a port we want. I won’t go into details here but there are two main things to look out for here:

  1. there are a number of reserved ports
  2. you need a port that is not in use

In order to check your port isn’t in use or to kick your previously running app out check out my port is taken post.

Testing the application:

Provided you were able to start the application above you would have received the following output:

request-capture service version v1.0.0 listening on port 8081
To terminate safely simply type `Control C`

The terminal will be blocked, waiting in the foreground. To test this open another terminal and run:

curl -i --request GET localhost:PORT/my/exampleA

Make sure you fill in the port above ^

The result should be:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 117
ETag: W/"75-arfZ/wgEnpY4RfORo9pLy4PbHHw"
Date: Tue, 17 Nov 2020 23:38:27 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"name":"request-capture","version":"v1.0.0","matchedpath":"/my/exampleA","url":"http://localhost:8081/my/exampleA"}

Next step is containerizing, let’s check it out, if you wish.

--

--

Matt
0 Followers

A curious mind in the tech field. #Question-everything