Introduction to Express js

What is Express JS?


Express is a fast, unopinionated and minimalist web framework for Node.JS.

So what does unopinionated and minimalist mean?

Unopinionated basically means that it's not a high-level framework(basic at it's core) and it doesn't assume how you're going to built the app like a certain design pattern you have full control of how you handle requests to the server and how you respond which is also why it's minimalist.

Express is a "server side" or "Back-End" framework.Express can be used with Front-End frameworks like React,Angular & Vue to built full stack applications.

Mostly Express is used to built the API so that it can take requests from the front end and it serves back data usually in JSON format which can be used to render the front-end.


Why use Express?

  • Makes building web applications with Node.Js much easier 
If you use Node itself to make the back-end it would be much harder and much more coding.
  • Used for both server rendered apps as well as API/Microservices.
  • Extremely light,fast and free.
  • Full control of request and response.
  • By far the most popular Node framework
  • Great to use with client side frameworks as it's all javascript.

Basic Server Syntax


Now let's look at the basic syntax of the web server made using express.


const express = require('express')

//Init express
const app = express()

//create your end points/route handlers
app.get('/', function(req, res) {
  res.send('Hello world');
});

//listen on port
app.listen(5000);


At the top we bring in express using common JS module syntax.And then to initialize have to set a variable to the express method( express() ) using named as app.After that you can create your endpoints.Here we have used a get request to the index route and then a callback function that takes in a request and a response which we can use get request parameters and to respond with whatever data we want.And finally we have to listen on a port, In this case we're using port 5000.Now if we open localhost/:5000 we should see "Hello World" response.


Basic Route Handling

app.get('/',function (req, res) {
//Fetch from database
//Load pages
//Return JSON
//Full access to request & response
});

So within your route you can fetch data from a database such as mongoDB, etc.You can load pages,return JSON data and you have full access to request & response.The request and reponse objects are very important. The request object represents things like URL parameters,query strings,any data that's sent within the body,http headers.Response object represents http response and it's upto you to send back JSON data,render templates etc with this response object.


  • simple routing
  • app.get(),app.post(),app.put(),app.delete(),etc


  • Express has a router so we can store routes in separate files and export
  • We can parse incoming data  with the Body Parser


Express Middleware

Middleware functions are functions that have access to request and response objects.Express has built in middleware but it also comes from 3rd party packages as well as custom middleware.


  • Execute any code
  • Make changes to the request/response objects
  • End response cycle
  • Call next middleware in the stack


























Comments

Popular posts from this blog

What is a Callback? - JavaScript

Difference between var,const and let in JavaScript

Introduction to NODE.JS