SlideShare a Scribd company logo
1 of 14
EXPRESS JS GENERATOR
Prepared By:
Mrs. Bareen Shaikh
Assistant Professor
Computer Science
MIT ACSC Alandi(D)
Pune
ExpressJS Generator
▪ What should be the ideal folder structure?
▪ Where should be the views placed?
▪ Where should the public assets be?
▪ How to link stylesheets?
▪ How should we create routes?
▪ How and where should the modules be?
▪ To create an application skeleton for above
use the application generator tool express-
generator.
ExpressJS Generator
▪ To create an application skeleton use the
application generator tool
 npm install express-generator -g
▪ This installs the Express generator as a global
package allowing you to run the express command in
your terminal
 express
Note if getting a error of.ps file then give following command
 Set-ExecutionPolicy RemoteSigned
▪ Create a application by application name as follows:
 express app1
▪ This creates a new Express project called app1, which is then
placed inside of the app1 directory.
Application Structure
▪ The generated Express application starts off with four
folders as follows:
▪ bin
 The bin folder contains the executable file that starts your app.
 It starts the server (on port 3000, if no alternative is supplied) and
sets up some basic error handling.
 npm start(start the new server)
Application Structure
▪ public
 everything in this folder is accessible to people
connecting to your application.
 In this folder, put JavaScript, CSS, images, and
other assets that will need to load your website.
▪ Routes
 This folder router files.
 It creates two files, index.js and users.js, which
serve as examples of how to separate out your
application’s route configuration.
Application Structure
▪ views
 The views folder is where you have the files used by your
templating engine.
 The generator will configure Express to look in here for a
matching view when you call the render method.
▪ Express is capable of handling server-side template
engines.
▪ Template engines allow us to add data to a view and
generate HTML dynamically.
▪ Pug is the new name for an old thing. It’s Jade 2.0.
▪ Due to a trademark issue, the name was changed from
Jade to Pug when the project released version 2 in 2016.
 npm install pug --save
Note on Template Engine
▪ A template engine enables to use static template
files in application.
▪ At runtime, the template engine replaces
variables in a template file with actual values, and
transforms the template into an HTML file sent to
the client.
▪ This approach makes it easier to design an HTML
page.
▪ Some popular template engines that work with
Express are Pug, Mustache, and EJS
▪ The Express application generator uses Jade as its
default, but it also supports several others.
Note on Template Engine
▪ If want to use latest updates to the template
engine then replace Jade with Pug in your app.
▪ Following application setting properties are to
be set in app.js in the default app created by
the generator:
▪ app.set('views', './views')
 This defaults to the views directory in the
application root directory.
▪ view engine, the template engine to use
engine:
 app.set('view engine', 'pug').
Note on Template Engine
▪ Create a Pug template file named index.pug in
the views directory with the following content:
 html
 head
 title= title body
 h1= message
▪ Then create a route to render the index.pug file
 app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello
there!' })
})
app.js
▪ This file sets up your Express application and
glues all of the different parts together.
 var createError = require('http-errors');
 var express = require('express');
 var path = require('path');
 var cookieParser = require('cookie-parser');
 var logger = require('morgan');
▪ The above files are require to run the express
generator application by using the
module.export concept of NodeJS
app.js
 var indexRouter = require('./routes/index');
 var usersRouter = require('./routes/users');
▪ The above lines of code require the different
route files that the Express generator sets up
by default: routes and users.
▪ var app = express() : create a new app by
calling express().
▪ The app variable contains all of the settings
and routes of application.
app.js
▪ Templating engine is set up for rendering views.
This is where you’d change the path to your view
files if necessary.
 app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'jade');
▪ Express being configured to use middleware.The
generator installs several common pieces of
middleware that you’re likely to use in a web
application:
 app.use(logger('dev')); app.use(express.json());
 app.use(express.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
app.js
▪ logger:When you run app all the routes that are
requested are logged to the console.
▪ express.json Require for parsing the body of
incoming HTTP requests, when JSON is sent via a
POST request and it puts this data in request.body.
▪ express.urlencoded: parses query string data in the
URL (e.g. /profile?id=5) and puts this in request.query.
▪ cookieParser.This takes all the cookies the client
sends and puts them in request.cookies. It also allows
to modify them before sending them back to the
client, by changing response.cookies.
▪ express.static.This middleware serves static assets
from public folder. If want to rename or move the
public folder,can change the path here.
app.js
▪ Route files that were required are attached to
our app.
 app.use('/', indexRouter);
 app.use('/users', usersRouter);
▪ Additional routes can be added here.
▪ Code after the above line is used for error
handling.
▪ Won’t have to modify this code unless you
want to change the way Express handles
errors.

More Related Content

Similar to Express Generator.pdf

Voorhoede - Front-end architecture
Voorhoede - Front-end architectureVoorhoede - Front-end architecture
Voorhoede - Front-end architectureJasper Moelker
 
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESSMERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESSannalakshmi35
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.jsdavidchubbs
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Eliran Eliassy
 
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Fwdays
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoasZeid Hassan
 
Protractor framework architecture with example
Protractor framework architecture with exampleProtractor framework architecture with example
Protractor framework architecture with exampleshadabgilani
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfBareen Shaikh
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsBudh Ram Gurung
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST APICaldera Labs
 
Nodejs first class
Nodejs first classNodejs first class
Nodejs first classFin Chen
 
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfAppster1
 
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfdokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfAppster1
 

Similar to Express Generator.pdf (20)

Voorhoede - Front-end architecture
Voorhoede - Front-end architectureVoorhoede - Front-end architecture
Voorhoede - Front-end architecture
 
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESSMERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.js
 
code-camp-meteor
code-camp-meteorcode-camp-meteor
code-camp-meteor
 
pio_present
pio_presentpio_present
pio_present
 
Workshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte IWorkshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte I
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
 
Protractor framework architecture with example
Protractor framework architecture with exampleProtractor framework architecture with example
Protractor framework architecture with example
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
 
Nodejs.meetup
Nodejs.meetupNodejs.meetup
Nodejs.meetup
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4
An Overview of Angular 4
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
 
RequireJS
RequireJSRequireJS
RequireJS
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Node js
Node jsNode js
Node js
 
Nodejs first class
Nodejs first classNodejs first class
Nodejs first class
 
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
 
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfdokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
 

More from Bareen Shaikh

More from Bareen Shaikh (10)

Middleware.pdf
Middleware.pdfMiddleware.pdf
Middleware.pdf
 
Express JS-Routingmethod.pdf
Express JS-Routingmethod.pdfExpress JS-Routingmethod.pdf
Express JS-Routingmethod.pdf
 
FS_module_functions.pptx
FS_module_functions.pptxFS_module_functions.pptx
FS_module_functions.pptx
 
File System.pptx
File System.pptxFile System.pptx
File System.pptx
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
NPM.pdf
NPM.pdfNPM.pdf
NPM.pdf
 
NodeJs Modules1.pdf
NodeJs Modules1.pdfNodeJs Modules1.pdf
NodeJs Modules1.pdf
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Introduction to Node JS1.pdf
Introduction to Node JS1.pdfIntroduction to Node JS1.pdf
Introduction to Node JS1.pdf
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 

Express Generator.pdf

  • 1. EXPRESS JS GENERATOR Prepared By: Mrs. Bareen Shaikh Assistant Professor Computer Science MIT ACSC Alandi(D) Pune
  • 2. ExpressJS Generator ▪ What should be the ideal folder structure? ▪ Where should be the views placed? ▪ Where should the public assets be? ▪ How to link stylesheets? ▪ How should we create routes? ▪ How and where should the modules be? ▪ To create an application skeleton for above use the application generator tool express- generator.
  • 3. ExpressJS Generator ▪ To create an application skeleton use the application generator tool  npm install express-generator -g ▪ This installs the Express generator as a global package allowing you to run the express command in your terminal  express Note if getting a error of.ps file then give following command  Set-ExecutionPolicy RemoteSigned ▪ Create a application by application name as follows:  express app1 ▪ This creates a new Express project called app1, which is then placed inside of the app1 directory.
  • 4. Application Structure ▪ The generated Express application starts off with four folders as follows: ▪ bin  The bin folder contains the executable file that starts your app.  It starts the server (on port 3000, if no alternative is supplied) and sets up some basic error handling.  npm start(start the new server)
  • 5. Application Structure ▪ public  everything in this folder is accessible to people connecting to your application.  In this folder, put JavaScript, CSS, images, and other assets that will need to load your website. ▪ Routes  This folder router files.  It creates two files, index.js and users.js, which serve as examples of how to separate out your application’s route configuration.
  • 6. Application Structure ▪ views  The views folder is where you have the files used by your templating engine.  The generator will configure Express to look in here for a matching view when you call the render method. ▪ Express is capable of handling server-side template engines. ▪ Template engines allow us to add data to a view and generate HTML dynamically. ▪ Pug is the new name for an old thing. It’s Jade 2.0. ▪ Due to a trademark issue, the name was changed from Jade to Pug when the project released version 2 in 2016.  npm install pug --save
  • 7. Note on Template Engine ▪ A template engine enables to use static template files in application. ▪ At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. ▪ This approach makes it easier to design an HTML page. ▪ Some popular template engines that work with Express are Pug, Mustache, and EJS ▪ The Express application generator uses Jade as its default, but it also supports several others.
  • 8. Note on Template Engine ▪ If want to use latest updates to the template engine then replace Jade with Pug in your app. ▪ Following application setting properties are to be set in app.js in the default app created by the generator: ▪ app.set('views', './views')  This defaults to the views directory in the application root directory. ▪ view engine, the template engine to use engine:  app.set('view engine', 'pug').
  • 9. Note on Template Engine ▪ Create a Pug template file named index.pug in the views directory with the following content:  html  head  title= title body  h1= message ▪ Then create a route to render the index.pug file  app.get('/', function (req, res) { res.render('index', { title: 'Hey', message: 'Hello there!' }) })
  • 10. app.js ▪ This file sets up your Express application and glues all of the different parts together.  var createError = require('http-errors');  var express = require('express');  var path = require('path');  var cookieParser = require('cookie-parser');  var logger = require('morgan'); ▪ The above files are require to run the express generator application by using the module.export concept of NodeJS
  • 11. app.js  var indexRouter = require('./routes/index');  var usersRouter = require('./routes/users'); ▪ The above lines of code require the different route files that the Express generator sets up by default: routes and users. ▪ var app = express() : create a new app by calling express(). ▪ The app variable contains all of the settings and routes of application.
  • 12. app.js ▪ Templating engine is set up for rendering views. This is where you’d change the path to your view files if necessary.  app.set('views', path.join(__dirname, 'views'));  app.set('view engine', 'jade'); ▪ Express being configured to use middleware.The generator installs several common pieces of middleware that you’re likely to use in a web application:  app.use(logger('dev')); app.use(express.json());  app.use(express.urlencoded({ extended: false }));  app.use(cookieParser());  app.use(express.static(path.join(__dirname, 'public')));
  • 13. app.js ▪ logger:When you run app all the routes that are requested are logged to the console. ▪ express.json Require for parsing the body of incoming HTTP requests, when JSON is sent via a POST request and it puts this data in request.body. ▪ express.urlencoded: parses query string data in the URL (e.g. /profile?id=5) and puts this in request.query. ▪ cookieParser.This takes all the cookies the client sends and puts them in request.cookies. It also allows to modify them before sending them back to the client, by changing response.cookies. ▪ express.static.This middleware serves static assets from public folder. If want to rename or move the public folder,can change the path here.
  • 14. app.js ▪ Route files that were required are attached to our app.  app.use('/', indexRouter);  app.use('/users', usersRouter); ▪ Additional routes can be added here. ▪ Code after the above line is used for error handling. ▪ Won’t have to modify this code unless you want to change the way Express handles errors.