Index
Overview


A video that accompanies the page is here....

The first thing we want to do is a minimal comm (communication) check to establish that there is comm between the consumer and provider.

We'll call this "Testing 1-2-3".

Note that we're leaving out authentification and other typical features to spotlight comm only.

API Provider


Code
The minimal provider is pretty simple:

  • Handle the expected url path "/testing123"
  • Get the result from the model
  • Serve (return) the result

We use JSON format as it is developer-friendly
processRequest() {
	this.logRequestInfo();
	this.route();
}

route() {
	const path = this.url.path();
	if (path === '/testing123')
		this.testing123();
	else
		log(`URL Path not handled: ${path}`);
}

testing123() {
	const model = new TestingTesting123();
	this.serve(model.getResults());
}

serve(results) {
	//serve in JSON format
	const res = this.response;
	res.writeHead(statusCodes.OK, jsonType);
	res.end(JSON.stringify(results));
}


API Consumer


Call API
Crucial piece here is the apiURL. It is like a phone number to the provider.

DataReceiver is a general helper object.
function callApi() {
	const
		apiUrl = 'http://localhost:3000/testing123';
		dataRecr = new DataReceiver(Controller.showApiResult);
	https
		.get(apiUrl, dataRecr.getReadDataFct())
		.on("error", errorCallback);
}

//entry point to this code
callApi();
Show Result
This method is in the class Controller

The result is simply printed to the console.
static showApiResult(results) {
	//we use stringify here as it produces a user friendly string
	log(JSON.stringify(results));
}


Source Code


The source code for this minimal example is here...

See the "README.txt" files.