Tuesday, October 30, 2018

JTD-DesignerSeries-9-NodeJS-101


Let's set some context
Web frameworks enables client-server architecture and Internet is a based on this client server model of computing. Usually there is a web server that listens for http request and the usual client is the browser like chrome. Every browser embeds a Javascript engine, and chrome is a C++ program which embeds Google V8 Javascript engine & makes available extra features like DOM manipulation through Ajax. Web Server, on the other hand, can support multiple technology stack like PHP, C#, Ruby & NodeJS. With NodeJS, you are writing code in Javascript and can share & use same libraries on the client & server side.

What is NodeJS
NodeJS is a server side technology and is a framework built with C++ & Javascript that adds features to the V8 runtime. NodeJS implements common traits of server side technology by wrapping C++ core implementation with Javascript wrappers.

Modules
a) Modules allows Node to organize code in reusable features with the help of require & module.exports constructs. Let's take a example and understand little bit.
app.js
var greet = require('./greet')
greet();

greet.js
var greet = function() {
console.log('Hello');
}
module.exports = greet;

In node implementation of require function, a module object is created, and a wrapper function on the module object passes the JS code to V8 runtime. During the execution of our code, a reference to greet function is assigned to module.exports which is returned from by the require function.This way, the greet function is exposed outside of the module.

b) When module is defined using multiple JS files, all files are placed in a folder and folder name is passed as parameter to the require function. Node will then check for index.js file in the folder which can require the modules defined in the folder.

EventEmitter
a) Event Emitter is a node module that allows you to register several listener functions associated with the event type. When an eventType is emitted, node invokes all listener functions listening for the eventType.

b) An object can inherit from the EventEmitter and then have access to the emit & on functions. Callbacks is a pattern with which you pass first class function and ask the function to invoke a callback after executing its code. Node uses an Event Loop outside V8 and enables developer to implement asynchronous behavior with EventEmitter, Callbacks & Event Loop.

File & fs
var fs = require('fs');
var readFile = fs.readfile (filename, function (err, data) {
console.log(data)
});
a) Node interfaces with the OS functions to read the data from the filesystem in an asynchronous way, and with the event loop check, it initiates a callback with the file data.

var readable = fs.createReadStream(readFilename);
var writeable = fs.createWriteStream(writeFilename);
readable.pipe(writeable);

b) With large datasets, file module provides streams that allows to read the file data in chunks and then use the pipe feature to push it to the writable stream.


Node being a Web Server
var http = require('http');
http.createServer(function(req, res) {res.writeHead(200, {'Content-Type': 'text/plain'}); res.send('Hello World\n');}).listen('3000', localhost);
a) Node abstracts the TCP/IP implementation & really simplifies the creation of a web server. It uses modules like http that require other _http modules along with Node C library (http_parser) and creates a listener on the socket. It then parses the request send by the browser in the req object & provide methods to create a response stream that maps to the standard http status code & mime-types.

b) Express is a node module that provides an easy abstraction to create the web server, enables robust routing features to handle http requests & create http responses. It also supports use of middleware that eases the features between request & response processing.

c) Template is a approach of inserting filler text along with static content in web pages, and during the web page rendering, template engine can process the filler text to generate dynamic content. Express supports quite a few template engines like Jade, EJS.

d) NodeJS makes it easy to with work with relational DB (MySQL) as well No SQL DB (MongoDB) with modules like mysql & mongoose. For eg: mysql node package transforms the resultset into an array of Javascript objects. Similarly mongoose node package provides allows you to define the structure of document with JSON & Schema function, and also provides the driver methods to retrieve & save data to MongoDB.


What is MEAN stack
Web Application is a combination of client side code, server side code, database system & bunch of other glue that connects everything to everything. MEAN stack provides the full stack features by allowing you to develop all these pieces of the App with MongoDB (M), Express (E), Angular(A) & NodeJS (N).
a) MongoDB - Is a NoSQL database technology that allows you to store data in the form of collections & documents.
b) Express - Is a Javascript framework that simplifies creating APIs, Routing by providing a middleware layer on top of NodeJS.
c) AngularJS - Is a Javascript framework that helps you manage the client side UI in the browser.
d) NodeJS - Is a Javascript server that helps you abstract the creation of http server, handling of requests & responses and also enable other server side technologies.

Code Examples
I) Render a index page with "hello" by running an express app on NodeJS.

In the example run we can see that browser renders the html body send by express app running on NodeJS server.

[DS-NodeJS-101-HelloWithNodeAndExpress]:https://github.com/madajee/JTD-DesignerSeries-9-NodeJS-101.git

II) Angular App rendering the Hello message returned from Node & Express App.

[DS-NodeJS-101-HelloWithAngularNodeAndExpress]:https://github.com/madajee/JTD-DesignerSeries-9-NodeJS-101.git
In the example run we can see that browser renders the he value of the message variable [Hello With Angular, Node & Express] defined in server side app.js file.


Todo Basic App with MEAN
Application [node-todo]: Let's build a To-Do list app with a MEAN stack.
[DS-NodeJS-101-TODO-MEAN]:https://github.com/madajee/JTD-DesignerSeries-9-NodeJS-101.git


Links & References
a) Learn & Understand NodeJS by Anthony Alicia on Udemy

No comments:

Post a Comment