Saturday, March 21, 2020

Introduction to ExpressJs

What is nodeJS?

  • Javascript runtime
  • Built on Chrome's V8 javascript engine.
  • Event driven, non-blocking IO model
  • Large package eco-system, npm!

Before we jump into ExpressJS, let's take a look at what NodeJS is quickly, this will help us understand why expressJS helps. NodeJS is a javascript runtime built on chrome's V8 javascript engine. It uses an event driven and non-blocking IO model which makes it lightweight and efficient. It also means that most of the operations are async and we'll be dealing with callbacks often. It has a large package eco-system, npm. So, most libraries you'll need for building a web app are built by someone already.


Starting a server in node.JS

    var http = require('http');

    http.createServer((req, res) => {
        res.write('hello world');
        res.end();
    }).listen(3000);

Writing a simple server hello world in NodeJS is pretty straight forward. NodeJS API provides a built-in http module, which let's you create a server. The createServer takes in a callback, which gets 2 parameters request and response. as the name suggests, request gives you info about the request and response helps us write a response, in most web apps to the browser. And here we listen to port 3000.


Starting a server in node.JS

    curl http://localhost:3000
   hello world

When you point your browser at localhost port 3000, you'll get a hello world. it's as simple as that.



What is Express?

Express.js is a light-weight Web framework for Node.js. Which means you can build web applications using JavaScript. Since it is light, you can do many things faster and with less lines of code, than a traditional Web application built using multiple technologies. Express provides a minimal interface to build our applications. It is flexible as there are numerous modules available on npm, which can be directly plugged into Express. It makes it easier to organize your application’s functionality with middle ware and routing; it adds helpful utilities to Node.js’s HTTP objects;it facilitates the rendering of dynamic HTTP objects.
Express is a part of MEAN stack, a full stack JavaScript solution used in building fast, robust, and maintainable production web applications.
Express was developed by TJ Holowaychuk and is maintained by the Node.js foundation and numerous open source contributors.

Why use Express

  • It is a framework, built on the top of http module of NodeJs.
  • Easy way to handle http requests.
  • Supports separate function for each url.
  • Easy to read parameters of both GET and POST requests.
  • Supports MVC Architecture. 


Features

·       Web Applications: Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
·       APIs: With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.
·       Performance: Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.
·       Frameworks: Many popular frameworks are based on Express.