0% found this document useful (0 votes)
24 views11 pages

Express Notes

Express.js is a web application framework for Node.js, developed by TJ Holowaychuk in 2010, that facilitates rapid development and management of servers and routes. It supports middleware for HTTP requests, dynamic HTML rendering, and various template engines like Handlebars. The document also covers installation steps, request and response objects, and examples of GET and POST methods.

Uploaded by

sutaraaditya36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Express Notes

Express.js is a web application framework for Node.js, developed by TJ Holowaychuk in 2010, that facilitates rapid development and management of servers and routes. It supports middleware for HTTP requests, dynamic HTML rendering, and various template engines like Handlebars. The document also covers installation steps, request and response objects, and examples of GET and POST methods.

Uploaded by

sutaraaditya36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Express.

js
- Developed by TJ Holowaychuk in Nov 2010
- Is a web application framework for Node.js
- Free and open source software
- License under MIT
- Built on the top of the Node.js that helps manage a server and
routes.

Features:
- Facilitates the rapid development
- Allows to set up middleware to respond to HTTP Requests
- Defines a routing table
- Allows to dynamically render HTML Pages
- Uses template engines

Installation:
Node.js should be installed on your machine.
1. Install express-generator

2. Create project in the particular folder using express command.

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
3. Install node libraries:

4. The folder structure will look like below given:

5. Start the node server using npm start command:

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
6. Go to browser and write localhost:3000 in the address bar to execute the
express code.

Express.js Template Engine:


A template engine facilitates you to use static template files in your
applications.

At runtime, it replaces variables in a template file with actual values, and


transforms the template into an HTML file sent to the client.

Some popular template engines are:

Pug (formerly known as handlebars haml-coffee


jade)
Mustache hogan ect

Dust jazz ejs


Atpl jqtpl haml

Eco hbs JUST

How to use hbs :

Express handlebars provides us HTML kind of environment so it is easy to

work with handlebars than jade engine.

1. Uninstall jade

2. Install hbs
npm install hbs --save

3. Make following changes in app.js file:


const hbs=require(‘hbs’)

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
4. Go to view folder change all jade files by hbs.
5. Remove all jade code and wite hbs code.

6. To print the value which is coming from routes we uses {{ }}.

Views/layout.hbs

Views/Index.hbs

Routes/index.js

7. Start the server using npm start and go to browser

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Express.js Request Object:
The express.js request object represents the HTTP request and has properties
for the request query string, parameters, body, HTTP headers, and so on.

Syntax:

router.get('/', function (req, res) {

// --

})

Express.js Request Object Properties:

req.body: It contains key-value pairs of data submitted in the request body. By


default, it is undefined, and is populated when you use body-parsing
middleware such as body-parser.

req.ip: It specifies the remote IP address of the request.

req.params: An object containing properties mapped to the named route


?parameters?. For example, if you have the route /user/:name, then the
"name" property is available as req.params.name. This object defaults to {}.

req.query: An object containing a property for each query string parameter in


the route.

Express.js Response Object:

The Response object (res) specifies the HTTP response which is sent by an
Express app when it gets an HTTP request.
Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Response End method:

This method is used to end the response process.


Syntax: res.end([data] [, encoding])

Response Get method:

This method provides HTTP response header specified by field.


Syntax: res.get(field)

Response JSON method:

This method returns the response in JSON format.


Syntax: res.json([body])

Response Redirect method:

This method redirects to the URL derived from the specified path, with
specified HTTP status
Syntax: res.redirect([status,] path)

Response Render method:

This method renders a view and sends the rendered HTML string to the client.
Syntax: res.render(view [, locals] [, callback])

Response Send method:

This method is used to send HTTP response.


Syntax: res.send([body])

Response Status method:


This method sets an HTTP status for the response.
Syntax: res.status(code)

Simple Express.js program with helpers:

Let’s demonstrates the example:

-print the value of variable


-check the condition
Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
-print values of an array
-Use hbs (handlebars) template engine.
Write the following code in index.js file:

Write the following code in view (index.hbs):

{{# }} {{/}} – is the syntax of helper classes.

Express.js GET Request:


Get method facilitates you to send only limited amount of data because data is
sent in the header. It is not secure because data is visible in URL bar.

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Let's take an example to demonstrate GET method:

To test this go to http://localhost:3000/10. The following response will be


displayed.

One more example with 2 parameters:-

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Pattern Matched Routes:-

This will only match the requests that have a 5-digit long id.

Express.js POST Request:

Post method facilitates you to send large amount of data because data is send
in the body. Post method is secure because data is not visible in URL bar but it
is not used as popularly as GET method. On the other hand GET method is
more efficient and used more than POST.

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Let's take an example to demonstrate POST method.

Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038
Infoway Technologies, 3rd Floor Commerce Centre, Rambaug Colony, Paud Road Pune 411038

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy