Android App Development

An Enterprise Application Workflow for Node

Every application we develop should follow some development life cycle and that is the relationship between the client and the server. A client is one which interacts with the server and the server always respond to the client, so that’s entirely master-slave relationship

I will now explain the whole enterprise web application workflow with considering the below image into the consideration.
fig1

Figure 1 Architecture Diagram of Enterprise web application
When you see the above image you can trace the relationship between client and server thereby we can divide the blog into the following categories

  • Client workflow
  • Middleware workflow
  • Backend workflow

While reading the below sections of the content you will be learning MEAN stack entire workflow of application which is our main objective in this blog.

Defining Client

In the above figure 1,

Client in the development platform will be in the form of a web/mobile application platform or in the most understanding format we can say it’s a browser.

Usually, the client will make requests to the server and server accepts the requests and gives back the response.
Client at our application side can take any of the below forms

  • Mobile platform (Android/IOS)
  • Web application platforms (Browser applications) like AngularJS, React.JS etc.

Generally, the client can make a request to the server in two-tier architecture or three tier architecture you can understand the client concept by looking into the below images
fig2

fig3
Figure 3 Three Client architecture

Defining Middleware Workflow and Backend workflow

In this section, we are going to learn how the application is involved in using the middleware components to and from the client and the database

Now we will divide the entire middleware unit into the following:

  • Defining RESTful API
  • Installing the MEAN Stack
  • Defining Mongo DB with Mongoose
  • Defining Express JS & Middleware
  • Defining Express JS routes

We are all set now! let’s go with the concepts one by one

Defining RESTful API?

REST Stands for Representational State Transfer object. As an architecture, it helps us to connect client to server through common interface.

Below are a few technical features.

  • Stateless client/server protocol: It means the Client and server will not be remembering any states
  • It can even manage the SOAP protocols.
  • Data transactions inside the REST protocol happens with the following four HTTP protocols POST(CREATE), GET(READ AND CONSULT), PUT (EDIT) & DELETE
  • Rest URI’s itself represent the objects and these objects have uniform interface.
  • Rest has the property called as idempotence that means how many times you call the REST URI you get the same response.

Find the below overview of HTTP methods of REST protocol


Installing MEAN (Mongo DB, Express.js, AngularJS(Front end platforms),Node JS) Stack

Installing MongoDB
https://acadgild.com/blog/an-introduction-to-mongodb-and-crud-manipulations/

Installing Node.JS
https://acadgild.com/blog/an-encyclopedia-to-node-js-and-its-environment/

Installing Express JS
Speaking about Express JS, it is the minimal and node.js web-app framework that provides awesome features for API endpoints.

I will write about this in detail in the later section of this blog.

We can install this framework using NPM(Node Packet Manager) globally so that it can create the web application using node terminal.

Type the below command
$ npm install express –save

Check the below screenshot for the result. Only typing this command will download all its dependencies.
fig4

Note:
We are not installing any angular stack in our web application. The motto behind is we can use this kind of backend development integration with any of the frontend platforms.

Defining MongoDB with Mongoose

It’s a NPM package which is helpful for a node interacting with the database.

Note: Installing the mongo and the node is compulsory before installing this mongoose package.

We have a very simple installation process.

Type the below command
$npm install mongoose

fig5

Defining Express.js and Middleware

Let’s dig the concept in detail by understanding the below figure
fig6

Take a look at the below points

  1. ExpressJS is a complete stack of web framework including HTML template solutions (jade, handlebars, Hogan.js) & CSS precompilers (less, stylus, compass).
  2. Middleware layers handle cookies, sessions, caching CSRF, compressions etc.
  3. Middleware is something that runs on each request made to the server and you can have many middleware (No limit).

Figure 04: explanation:

  1. At the left-hand side, you have the client (which is your web browser) and will make the http request(GET).
  2. The request goes to the Express.js stack wherein we have many middleware components to handle the requests.
  3. You can see the middleware: app.use(…) and this is the middleware stack which is comprising of cookie parser to handle the cookies and body parser to pull the information from the HTML post, logger to trace the request logging parameters and many more.
  4. Request is taken care by handlers to add the request parameters by passing it to the URL
  5. Routers here play an important role in routing the handlers to the specific methods which consist of HTTP methods.
  6. res.render() sends back the HTTP response.
  7. Remember the middleware components can handle request and response.
  8. Finally, HTML page is rendered.

Let’s give some examples of the Middleware components

If you want to log the IP of the client on each request:

app.use(function (req, res, next) {
var ip = req.headers[‘x-forwarded-for’] || req.connection.remoteAddress;
console.log(‘Client IP:’, ip);
next();
});

Notice that each middleware has 3 parameters:

  1. req: Contains all the requests objects like URLs and path.
  2. res: It is the response object where we can send the reply back to the client.
  3. next: Continues with the next middleware in the chain.

Take a look at the one more example

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(morgan(‘dev’));

.use() is the method to configure the middleware used by the routes of the Express Server object
app.use : It is the middleware function which gets executed when the base path matches.

Usage of GET and POST method.

GET
Have a look at the below code

app.get(‘/’,function(req,res){
res.send(‘Hello Android Node whats up!!’);
});

When the middleware component use the GET request with the matching routes, you get the response with res.send(“ ….. ”); and therefore ending the middleware chain.
Have a look at the below screenshot as the response.

POST
Have a look at the below screenshot of the code

Post is a HTTP method which is used to CREATE the database.

Router handles the POST method to a specific API endpoint along with taking the request parameters with checking to and from the database.

I will show the usage of this API when I explore the POSTMAN tool.

Defining Express JS routes

The style of ExpressJS routes is actually a basic routing

Technically routing is done to determine how an application responds to a client request to a particular API endpoint using the HTTP methods (GET, POST and So on)

Basically a routes is having one or more handler functions which is executed when the route is matched.

The following is the syntax:

Below are some examples which you can get cleared with it.

Example 1: app.get(‘/’,function(req,res)){
res.send(‘Hello world!!!!!!!’)
});

Example 2: Respond to POST request on the root route(‘/’)
app.post(‘/’,function(req,res))
{
res.send(‘Hello post request’);
});

Example 3: Respond to the PUT request to the /student route
app.put(‘/student’,function(req,res)){
res.send(‘Hello!!!!!! Got a post request to the /student route’);
});

Example 4: Respond to the Delete request to the /student route
app.delete(‘/student’,function(req,res)){
res.send(‘Hello!!!!!! Got a delete request to the /student route’);
});

Readers! Now you are at the second stage of understanding with node application workflow.
Still development part is awaited in the next blog.

NodeJS course details

Hope you enjoyed going through the architecture of Node and it helped you understand the basics workflow required for the application.
Thanks for reading! Keep visiting www.acadgild.com for more updates on the technical and certification courses.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close