Express Backend

This is a back-end Node.js framework for building web applications.
to help with you with a lot of common tasks especially dealing with REST APIs.

link - 
link -

npm install express 

Basic Routing

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 (GET, POST, and so on).
Each route can have one or more handler functions, which are executed when the route is matched.
Route definition takes the following structure:

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.

Route Methods

A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.
Express supports methods that correspond to all HTTP request methods: get, post, and so on.
Respond to GET request on the root route (/), the application's home page:

app.get('/', (req, res) => {  
  res.send('GET request to the homepage')

Respond to POST request on the root route (/), the application's home page:'/', (req, res) => {  
  res.send('POST request to the homepage')

Respond to a PUT request to the /user route:

app.put('/user', (req, res) => {  
  res.send('Got a PUT request at /user')

Respond to a DELETE request to the /user route:

app.delete('/user', (req, res) => {  
  res.send('Got a DELETE request at /user')

Respond with Hello World! on the homepage:

app.get('/', (req, res) => {  
  res.send('Hello World!')

Route Paths

Route paths, in combination with a request method, define the endpoints at which requests can be made.
Route paths can be strings, string patterns, or regular expressions.

This route path will match requests to /about.

app.get('/about', (req, res) => {  

This route path will match acd and abcd.

app.get('/ab?cd', (req, res) => {  

Route Parameters

Route parameters are named URL segments that are used to capture the values specified at their position in the URL.
The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.
The name of route parameters must be made up of "word characters" ([A-Za-z0-9_]).

Route path: /users/:userId/books/:bookId 
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.

app.get('/users/:userId/books/:bookId', (req, res) => {  

Route Handlers

Route handlers can be in the form of a function, an array of functions, or combinations of both, as shown in the following examples.
You can provide multiple callback functions that behave like middleware to handle a request.

app.get('/example/b', (req, res, next) => {  
  console.log('the response will be sent by the next function ...')
}, (req, res) => {
  res.send('Hello from B!')

Response Methods

The methods on the response object (res) in the following table can send a response to the client, and terminate the request-response cycle.
If none of these methods are called from a route handler, the client request will be left hanging. - Prompt a file to be downloaded. 
res.end() - End the response process.
res.json() - Send a JSON response.
res.jsonp() - Send a JSON response with JSONP support.
res.redirect() - Redirect a request.
res.render() - Render a view template.
res.send() - Send a response of various types.
res.sendFile() - Send a file as an octet stream.
res.sendStatus() - Set the response status code and send its string representation as the response body.


You can create chainable route handlers for a route path by using app.route().

  .get((req, res) => {
    res.send('Get a random book')
  .post((req, res) => {
    res.send('Add a book')
  .put((req, res) => {
    res.send('Update the book')


Use the express.Router class to create modular, mountable route handlers.
A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a "mini-app".

Express Application

Creates an Express application.
The express() function is a top-level function exported by the express module.

const express = require('express')  

Method - express.json([options])
It parses incoming requests with JSON payloads and is based on body-parser.

Method - express.raw([options])
It parses incoming request payloads into a Buffer and is based on body-parser.

Method - express.Router([options])
Creates a new router object.

Method - express.static(root, [options])
It serves static files and is based on serve-static.

Method - express.text([options])
It parses incoming request payloads into a string and is based on body-parser.

Method - express.urlencoded([options])
It parses incoming requests with urlencoded payloads and is based on body-parser.

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext