Load HTML pages using Express Js | Express JS Tutorial | Node Js | Learning Points

Learning Points
23 Feb 202207:58

Summary

TLDRThis tutorial covers the basics of setting up an Express.js server, installing Nodemon, and implementing routing. It demonstrates how to create HTML pages like 'index.html', 'blog.html', and 'contact us.html' in a 'public' folder and serve them using Express. The instructor guides viewers on how to access the public folder and render HTML files dynamically. The video also teases upcoming content on template engines like EJS for Express.js, promising to build real-life applications in future tutorials.

Takeaways

  • 🛠️ Discussed installation of Express and Nodemon, and routing concepts in Express.
  • 📂 Created a 'public' folder to store HTML files for the web application.
  • 📄 Created three HTML files: index.html, blog.html, and contact-us.html.
  • 💻 Demonstrated how to render HTML pages using Node.js Express framework.
  • 🔧 Used the 'path' module to access the 'public' folder and its contents.
  • 📝 Explained the importance of using constants for directory paths.
  • 📡 Showed how to send HTML files as responses to browser requests.
  • 🔄 Mentioned the need to restart Nodemon to see changes in HTML files.
  • 🎥 Announced upcoming discussions on template engines like EJS in Express.
  • 🌐 Highlighted the potential to create real-life web applications using Express and EJS.

Q & A

  • What did the video discuss about Express and Nodemon installation?

    -The video discussed how to install Express and Nodemon, which are essential tools for setting up a Node.js application.

  • What is the routing concept in Express?

    -The routing concept in Express refers to defining paths for client requests and linking them to appropriate handler functions to process the requests.

  • How can you create an HTML page using Express?

    -You can create an HTML page by creating a folder named 'public' and placing your HTML files inside it. Express can then serve these files when requested.

  • What is the purpose of creating a 'public' folder in an Express application?

    -The 'public' folder is used to store static files like HTML, CSS, and JavaScript files that are served to the client without any server-side processing.

  • How do you access the 'public' folder in an Express application?

    -You can access the 'public' folder by using the 'path' module in Node.js to construct the directory path and then serving files from that directory using the 'sendFile' method.

  • What is the significance of using constants in the script?

    -Using constants, such as for the path to the 'public' folder, ensures that the value cannot be accidentally changed or overwritten, which can lead to more stable and predictable code.

  • How do you serve an HTML file like 'index.html' in Express?

    -To serve an HTML file, you use the 'response.sendFile' method in your route handler, specifying the path to the file relative to the 'public' folder.

  • Why is it necessary to restart Nodemon when changes are made to the HTML files?

    -Restarting Nodemon is necessary to ensure that the latest changes to HTML files are reflected in the application, as the server needs to be reloaded to pick up new static file changes.

  • What is the next topic the video series will cover after discussing HTML rendering?

    -The next topic in the video series will be about template engines in Node.js Express, specifically focusing on EJS (Embedded JavaScript templates).

  • What are some features of Express.js that will be discussed in upcoming videos?

    -Upcoming videos will discuss features of Express.js such as middleware, routing, template engines, and how to create real-life applications using these features.

Outlines

00:00

💻 Setting Up HTML Pages with Node.js Express

The script discusses how to set up HTML pages using Node.js Express framework. It starts by mentioning the installation of Express and Nodemon, and the concept of routing in Express. The speaker then guides on creating a 'public' folder to store HTML files such as 'index.html', 'blog.html', and 'contact us.html'. Each file is briefly described with a simple HTML boilerplate and specific titles. The script emphasizes the simplicity of serving HTML files using Express and demonstrates how to access the 'public' folder and serve the 'index.html' file. It also introduces the concept of using 'path' to navigate directories and serve files, suggesting the use of constants for better maintainability.

05:00

🌐 Serving HTML Files in Express

This paragraph explains how to serve HTML files using Express. It details the process of sending responses to the browser, specifically how to send HTML files using the 'response.sendFile' method. The script provides an example of how to dynamically construct file paths using template literals and the 'publicPath' variable. It also touches on the necessity of restarting Nodemon to reflect changes in HTML files. The speaker demonstrates accessing different pages like 'index.html', 'blog.html', and 'contact us.html' by navigating to their respective URLs. The paragraph concludes with a teaser for upcoming content on template engines like EJS within the Express framework, hinting at future tutorials on creating real-life applications with Node.js.

Mindmap

Keywords

💡Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. In the video, Express is used to create a server that serves HTML pages. It is fundamental to the video's theme of teaching how to build web applications with Node.js.

💡Nodemon

Nodemon is a tool that helps develop Node.js applications by automatically restarting the node application when file changes in the directory are detected. It is mentioned in the script as a way to refresh the server when changes are made to the HTML files.

💡Routing

Routing refers to the process of defining paths through which users can reach specific resources within a web application. In the video, routing is discussed in the context of Express, showing how to set up paths to different HTML pages.

💡HTML

HTML, or HyperText Markup Language, is the standard markup language for creating web pages. The script describes creating HTML files like 'index.html', 'blog.html', and 'contact us.html', which are then served by the Express application.

💡Public Folder

A 'public' folder is a directory in a web application that typically contains static files like HTML, CSS, and JavaScript that are accessible to the client. In the video, the 'public' folder is used to store HTML files that Express serves to the browser.

💡Path

Path refers to the location of a file or directory in a file system. The script explains how to use Node.js's 'path' module to navigate to the 'public' folder where the HTML files are stored.

💡Template Literals

Template literals are a feature in JavaScript that allows embedding expressions within a string, using a template-like syntax. In the script, template literals are used to dynamically construct file paths for serving HTML files.

💡Response

In the context of web development, a 'response' refers to the data sent from the server to the client in response to a request. The video script describes using Express to send HTML files as responses to client requests.

💡EJS

EJS, or Embedded JavaScript templating, is a templating engine that allows for the creation of dynamic HTML pages. The script mentions that future videos will discuss EJS, indicating it as an alternative to static HTML for more interactive web applications.

💡Template Engine

A template engine is a server-side software tool that combines template files with data to produce HTML files dynamically. The script hints at the use of template engines like EJS in Express applications for generating HTML on the fly.

💡Node.js

Node.js is a JavaScript runtime that allows developers to write server-side JavaScript code. The video is centered around using Node.js to create a web server with Express, demonstrating the creation and serving of HTML pages.

Highlights

Introduction to installing Express and Nodemon.

Discussion on routing concepts in Express.

Creating a simple HTML page using Node.js Express framework.

Creating a folder named 'public' for storing HTML files.

Creating 'index.html', 'blog.html', and 'contact us.html' files.

Using HTML boilerplate template for creating pages.

Writing content for 'home', 'blog', and 'contact us' pages.

Accessing the 'public' folder in Node.js.

Using 'path' module to navigate through directories.

Creating a constant variable for the 'public' folder path.

Sending HTML pages to the browser using Express.

Using 'response.sendFile' to serve HTML files.

Accessing 'index.html', 'blog.html', and 'contact us.html' through routing.

Refreshing the page to see updates without restarting Nodemon.

Demonstrating how to render HTML files through Express.

Teaser for upcoming discussion on template engines in Node.js Express.

Introduction to using '.ejs' files for templating in Express.

Announcement of future tutorials on creating real-life applications with Node.js.

Invitation to like and subscribe for upcoming videos.

Transcripts

play00:00

hello friends last day we discussed

play00:02

about the uh how to install express how

play00:05

to install nodemon and we discussed

play00:07

about the routing concept in express and

play00:10

here we are getting some things here so

play00:12

here we are getting the home page and if

play00:15

i just want to get the blog link so

play00:17

we'll get the blog right so now

play00:20

if i just want to create a html page and

play00:23

if i want to load that then how you can

play00:25

do this right simple node.js express

play00:28

framework provides the simple things so

play00:31

let's create some html files so

play00:34

just go here and let's create a folder

play00:37

that is suppose public and inside the

play00:40

public folder we will store all the

play00:43

things so

play00:44

just create a folder

play00:46

public and inside the public folder

play00:49

let's create a

play00:52

index dot

play00:54

html

play00:55

or let's create another two files that

play00:58

is home

play00:59

sorry home not home that is blog.html

play01:03

or you can write contact us

play01:07

dot

play01:08

html

play01:10

right so in this way we are creating

play01:12

three files and let's

play01:14

use the html boiler template and simply

play01:18

just i am not going to

play01:19

explain all the designing part but i

play01:22

want to show you how to render the html

play01:24

page through the node.js so simply write

play01:28

suppose home

play01:31

and inside the h1 tag just write

play01:34

this is home page

play01:38

right and just copy all those things and

play01:41

paste it here

play01:43

also here and let's change the title

play01:46

contact us

play01:48

also write this is contact us

play01:51

page

play01:52

and also here this is

play01:55

blog

play01:56

and this is blog page all right so these

play01:59

three files are here now the thing is

play02:03

how we can access this index.html or

play02:06

blog.html so basically in node.js if i

play02:10

can access this folder

play02:12

that means we can access all those

play02:14

things right so for now i just closing

play02:17

all the things

play02:18

hello homepage right so i just

play02:21

removing this thing and inside the here

play02:25

we will just load the html page right so

play02:28

i just remove all those things we don't

play02:30

need this so actually to send anything

play02:33

to the browser we going to use the

play02:35

absent and now we are going to send the

play02:37

html page so we'll have some different

play02:40

methods so before that let's try to

play02:43

access this public folder right so

play02:45

inside the public folder all the things

play02:47

here

play02:48

so to access this so let's require the

play02:52

path so simply create another uh

play02:55

constant variable so simplify constant

play02:59

path

play03:00

or you can use lead very things like no

play03:02

matter so if you change this as a

play03:04

constant it will be more better right so

play03:06

why we are using constant basically

play03:09

constant cannot be overwritten and one

play03:12

time it's can be declared right so

play03:14

simply write path equal to

play03:16

uh

play03:17

require

play03:19

path

play03:19

okay so path is included and let's use

play03:23

this so how can we use this so simply

play03:26

just console

play03:28

dot log

play03:30

and let's check what we actually getting

play03:34

in the directory name so if i just use

play03:36

simply this javascript things that is

play03:39

the underscore underscore

play03:41

d

play03:42

name

play03:43

means director name so just go here we

play03:46

can see

play03:47

home

play03:48

hp desktop node express right so inside

play03:53

this directory name we are getting all

play03:55

those things

play03:56

now we need to go inside node express

play04:00

slash

play04:01

public folder so how we can do this

play04:03

simply we can add the public here

play04:06

so here what path actually doing so

play04:09

let's create another variable so simply

play04:13

write let

play04:14

and i want to create a variable like a

play04:16

public path right so we will use this

play04:19

public path everywhere and let's use

play04:21

path

play04:22

dot

play04:23

join that is direct to name and my

play04:26

folder name is public so let's add this

play04:30

now if i just going to console log these

play04:32

things

play04:33

let's see how we getting or what

play04:36

actually we are getting

play04:38

public path so just here we are getting

play04:41

the node express public so that is the

play04:44

important things so we can access this

play04:47

right now the thing is going to use the

play04:51

index.html when we just run these things

play04:54

and we need to use the blog html if i

play04:57

just use slash blog.html

play05:00

so simply

play05:01

just

play05:02

use

play05:03

response so i want to send a response to

play05:06

the browser so simply write response and

play05:09

you need to send the file so simply

play05:12

write

play05:12

send file and we'll use

play05:16

this public path

play05:18

and here simply use the template literal

play05:21

and use this curly braces

play05:25

and we will use index

play05:28

dot html right and in the same way just

play05:32

copy this

play05:34

and inside here public path actually we

play05:37

are getting the full directory and the

play05:39

public and then we are just calling the

play05:42

file name that is for homepage we are

play05:44

calling index.html for blog we just uh

play05:47

calling blog.html

play05:50

and for contact us we are just calling

play05:52

contact us dot html

play05:55

right so

play05:57

for now we don't need console log so

play05:59

actually we tested it what is inside the

play06:01

console log so simply close this

play06:05

with no need of these things so simply

play06:07

remove this right

play06:09

and as we are changing in the

play06:12

html file sometime is not loaded so

play06:15

let's restart the nodemon once again

play06:19

and we can get the output so simply just

play06:22

refresh this this is

play06:24

block page and this is block and if i

play06:27

just use simply slash or we can see this

play06:31

is home page so basically these things

play06:34

actually coming from here this is home

play06:37

page and if i just use the contact us so

play06:40

we'll get the contact us also right so

play06:44

in this way we can render any html file

play06:47

through the express and it's very simple

play06:52

so hope you will like this video and

play06:54

still if you have any queries about this

play06:57

video please comment in below comment

play06:59

section next day we are going to discuss

play07:02

about one of the template engine in the

play07:05

node.js express framework right so as we

play07:07

are using the dot html file so inside

play07:10

the express we have a dot ejs file so we

play07:14

will discuss about that things from our

play07:16

upcoming videos so it may have to see

play07:18

tutorial on eds and we can create a real

play07:22

life application so if i just want to

play07:24

create any web application or any

play07:27

websites using node we can use the

play07:30

node templating that is the express

play07:32

templating actually ejs we can use ejs

play07:35

so

play07:36

from next we are going to start about

play07:38

the ejs and we can discuss about the

play07:41

features of express js like a midi lower

play07:44

and there is lots of things so we'll

play07:46

discuss these things step by step

play07:48

so if you love this video please like

play07:51

and subscribe this channel for upcoming

play07:52

video thanks for watching this video

play07:55

have a good day bye

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Node.jsExpressNodemonRoutingHTMLWeb DevelopmentJavaScriptTutorialBlogContact Us
¿Necesitas un resumen en inglés?