Recent Posts

Express.js


                 
GETTING STARTED WITH EXPRESS.JS

What is Express.js?

  • Express.js is a backend framework designed to develop complete web applications and api's.

Installing Express.js

  • Express.js gets installed via the Node Package Manager. This can be done by executing the following command.

  • npm install express

Let's use our newly installed Express framework and create a simple "Hello World" application.

var express=require('express');
var app=express();
app.get('/',function(req,res)
{
res.send('Hello World!');
});
var server=app.listen(3000,function() {});

Express.js has two main objects when handling client-server communication. One is the "Request object" which contains information specifically related to HTTP.such as the HTTP header, body, and query parameters.

Routing refers to determining how an application responds to a client request to a particular endpoint.Which is a URI (or path) and a specific HTTP request method. 

Each route can have one or more handler functions, which are executed when the route is matched.

Route definition takes the following structure

:app.METHOD(PATH, HANDLER)

Where:

  • app is an instance of express.
  • METHOD is an HTTP request method, in lowercase.
  • PATH is a path on the server.
  • HANDLER is the function executed when the route is matched..
The following examples illustrate defining simple routes.

app.get('/', function (req, res) {
  res.send('Hello World!')
})
app.post('/', function (req, res) {
  res.send('Got a POST request')
})
app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user')
})
app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user')
})

Status codes and redirects:

  • HTTP status codes are used extensively in production-grade Express applications, as they provide a mechanism for the server to issue a basic response to the client.

Status code ranges:

  • 100s Informational
  • 200s Success.
  • 300s Redirection
  • 400s Client error
  • 500s Server error

No comments

If you have any doubts, Please let me know