zaterdag 20 september 2014

Node.js reading json from rest service

Introduction:

The concept of a java script server is in my opinion revolutionary because of the fact that frontend technology also can be separated from the backend as a specialism. But what good is the one without the other. Creating a presentation  is no good without data. But what good is data if we cannot present it. So in this matter  I would show you that it is possible to combine the best of both worlds.


The solution:

In my previous post I described how to install a nodejs server and create an hello world application.
Because connecting to a rest service is a bit more complex we need to do some actions before we can start:

create an folder for example rest-api
cd rest-api

We need the superagent library:
npm install superagent@0.17.0 --save-dev

After this we can start building up the server:
// initializing the http package.
var http = require("http"),
//making the port available through a parameter 
port = process.argv[2] || 8888;
// initializing the superagent package
var superagent = require('superagent');
// creating the http server
http.createServer(function(request, response) {
//retrieving the rest url throught the superagent package
            superagent.get('http://localhost:8080/hello')
                .end(function(e, res){
//writing the res.text response from the rest server to the response which is the browser client response.
                    response.write(res.text, "binary");
                    response.end();
                })
//make the server listen to the port that is declared in my case 8090 or the standard port 8888
}).listen(parseInt(port, 10));
//just a friendly console message.
console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");

Conclusion:

The world of javascript is not that different from the world of Java these days. There is this virtual machine, it is getting Object Oriented in a kind of way and you have to know your way in the library wilderness. This makes the gap between the two worlds smaller. There are still differences between the way of working between these kind of frontend technologies and Java as allways the devil is in the details.

Have fun!

vrijdag 5 september 2014

Hello world with node.js

Introduction:

In my previous post I wrote about the clean separation of concerns by using a javascript server like node.js. In the past I worked on a project where they used node.js but personally I never got involved. I wrote the backend part of the system where we used rest services that returned json as an answer for the node.js server (javascript can handle json). 

So curiousity killed the cat but made man kind evolve. So I tried to set this up on my linux machine. Googled around a bit and found the answers to my questions. 

Installation:

actually on linux it is pretty basic if you have git running:
My attempt under debian:


  • git clone --depth 1 git://github.com/joyent/node.git
  • cd node
  • git checkout v0.4.11
  • export JOBS=2
  • mkdir ~/local
  • ./configure --prefix=$HOME/local/node
  • make
  • make install
  • echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
  • echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
  • source ~/.profile

My attempt under ubuntu:
  • sudo apt-get install curl
  • curl -sL https://deb.nodesource.com/setup | sudo bash -
  • sudo apt-get install nodejs
(The path under ubuntu will bie /usr/bin/node)


After you done litterly this (take care of the $PATH ELEMENTS. You need to replace them with the real path where you put the node server). 
after this you can create a test.js in any folder you like containing:
//require is the import in java
 var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
//creating the http server
var server = http.createServer(function (request, response) {
//bare writing to the response like in the old days with Java.
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8090);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8090/");
After you saved the file you have to tell nodejs that he has to start this application:
  1. ./node test.js in your console in the node directory
  2. Start a browser on the desired port 8090 in my case
The result should be hello world on a snowy white page.

have fun!

dinsdag 2 september 2014

separating the frontend from the backend

Introduction

Through the years I seen a couple of application architectures. Some of them had the multi tier design but where  still one war file. I never seen that as a bad design. That was the best available way. The fun part of a way like that was that the architect had to defend the borders between the layers. That is where the weak point in this architecture lies. It is easy for a developer, for what ever reason, to cross the borders and start mixing the responsibilities of the different tiers. This is the first step of downgrading an well setup application to a kind of chaos architecture.

As allways in time ideas improve. A couple of years ago we suddenly had SOA. The backend logic was pushed back into REST or SOAP services with a separate project that was the frontend. The downside of this architecture was, that the html code was still infected with tags from java frameworks. Fore example
JSF had is own XHTML and even the newer like Wicket have there own tags. The most Frontend guys dont like that. They know html and they dont want to know about all the java frameworks.

The other downside of weaving the java frameworks into html is: that if the compagny decides that in some cases it should not be Java but something like PHP or Ruby etc... they still endup with a java frontend framework.

Now we enter a time where the possibilities to separate the frontend from the backend in a sharper and smarter way. It is possible to run a frontend tier completely on a javascript based server like node.js. Here is a list of js solutions.

The architecture:


Behind teh node js server part you can build as many java rest services as needed. Now we have a complete separation from the frontend and the backend. On the node.js server it is also possible to declare small html objects like textboxes or buttons etc...

Conclusion:

There are allready compagnies working on a application architecture like this or have in place. It requires to see the frontend as separate specialism and for that you need people that know how to deal with a frontend architecture like this. As a backend developer you have to let te responsibility for the frontend go and concentrate on what you can do actually the best backend development.

Have fun!