⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Tuesday, November 11, 2025

Get Data from Backend (NodeJS) to Frontend

 Part 2, to the 'Build a Webscraper (super simple!) video. In this video, I show you how to get data from your NodeJS backend into your super simple JavaScript frontend.

0:00

hello everyone so in the last i

0:02

showed you how to build a web scraper by

0:05

building a nodejs application in which

0:08

we scraped a website and got the data

0:10

showing up in our terminal but how do we

0:13

get that data showing up in our browser

0:16

or in other words how do we get the data

0:18

from here showing up here so that we can

0:22

use it in our front ends well we're

0:25

going to do this using a package called

0:27

express that we installed in the last

0:30

video for those of you who are new to

0:32

joining us please feel free to watch

0:35

part one in the description below before

0:37

watching further or simply join us where

0:40

we left off i will be doing a brief

0:42

recap of what we have coded so far

0:45

coming up next

0:47

in part one we imported the packages

0:50

axios and cheerio in order to scrape the

0:52

website guardian.com

0:54

we did this by passing through the url

0:57

into axios and once the response came

1:01

back to us we got the response data and

1:04

used cheerio to pick out certain html

1:06

elements to create an object so this

1:10

object right here and pushed it into our

1:13

articles

1:14

array

1:15

that we then console logged out

1:18

the result of the console log showed up

1:20

in our terminal as we are currently

1:22

working in the back end

1:24

it is also important to note that this

1:26

code will only run if i start the script

1:29

which i have written

1:32

here

1:33

okay so to start this script all i'm

1:35

going to do is write npm run

1:39

start

1:40

just like so this script relies on a

1:44

package called no demand that

1:46

essentially listens out to any changes

1:48

we make on port 8000 and as you will see

1:51

here in the terminal these are the

1:52

results of our website scrape okay so

1:56

that is a very quick recap of what we

1:58

did for anyone new joining us here if

2:00

you would like a more in-depth

2:02

explanation of the packages and what is

2:03

happening please do watch part one

2:06

otherwise let's carry on so i'm just

2:08

going to close that and go back to our

2:10

index.js file

2:12

now this video is going to focus heavily

2:15

on express and routing the word routing

2:18

refers to how an application's points or

2:22

in other words this right here

2:25

respond to client requests

2:27

so for example if my client or in other

2:30

words my computer makes a get request to

2:33

this url right here i can make it return

2:37

something back to me and i can do this

2:40

thanks to express here is the structure

2:43

that you will see while rooting so app

2:47

a method

2:48

a path

2:50

and a handler as we mentioned in the

2:53

last video app is what holds all of our

2:56

express wonderfulness and comes with

2:59

great things such as

3:01

app

3:02

listen app get

3:05

or app post

3:07

app on this occasion is also referred to

3:10

as an instance of express

3:13

the method that you see here refers to

3:17

an http

3:18

request method if you know about apis

3:21

and how to use restful apis you will

3:23

know that the most popular types of

3:25

requests are the get

3:27

post put and delete requests

3:30

we are going to use the get request so

3:33

i'm going to replace this with get well

3:35

let's just write it out here

3:37

get we're going to use the

3:39

get request

3:41

to get data from a certain resource or

3:44

path we use the

3:47

host request

3:49

to add data to a certain path or

3:53

resource and we use the

3:55

put

3:56

request

3:58

to edit data from a certain resource

4:01

okay so edit

4:03

add data

4:05

get data and of course we use the delete

4:08

method

4:09

to delete

4:12

data from a certain resource

4:15

okay so all of these come with express

4:19

we are getting them from here

4:22

just gonna delete that for now

4:24

next we have the path

4:26

this refers to the path on the server we

4:29

will in fact be defining this ourselves

4:31

later on

4:32

and finally we have the handler which is

4:35

essentially a callback function that

4:37

gets executed when we visit this part

4:41

okay so say my path was localhost

4:44

8000 for slash

4:47

burgers and i visited this in the

4:49

browser the function here whatever i

4:53

write will be executed

4:55

okay so we will be doing this later

4:59

okay

5:00

so great hopefully that makes sense now

5:02

we have the template of how to do basic

5:05

routing in express so let's try out

5:08

ourselves

5:09

let's start off small and simply decide

5:11

that if i visit a local host a thousand

5:14

it must be eight thousand as that is

5:16

what we defined as our port in the back

5:19

end here then i want the word this is my

5:22

web scraper to show up so i would do so

5:25

like so i'm going to use this template

5:27

so app and then the method i want to use

5:30

is the get

5:32

method

5:33

the part where we have just said it is

5:35

localhost 8000

5:37

or we can actually just do this for the

5:40

home page that will be the same and then

5:43

we need the handler so we know that the

5:45

handle is a function so let's write

5:48

function

5:50

that is my function and what happens in

5:52

my function well i just want to show

5:55

hello world so for this i'm actually

5:57

going to pass through a request and

5:58

response

6:01

just like so this is being added by

6:04

webstorm is a helper i have essentially

6:06

just passed through the parameters

6:08

request with a comma and response and if

6:11

i get the response and use json i can

6:14

then pass through a string so this is my

6:18

web scraper

6:20

and this is what the response will be

6:23

when i visit my localhost 8000 page so

6:27

let's check it out i'm going to

6:29

gravitate to the browser

6:32

localhost

6:35

8 000

6:37

there we go this is my web scraper so if

6:41

i go back here

6:43

let's change this

6:45

just to be

6:46

anya click save

6:49

refresh

6:50

and there we go so we are now getting

6:52

strings show up when we visit localhost

6:55

8000. once again i'm just going to

6:57

switch this back

7:00

there we go

7:02

okay so this is the response

7:05

hence i've used response and not request

7:08

it's the response that i want to show as

7:11

this is my web script

7:13

great

7:14

so now that we have a basic

7:16

understanding of how to show text in the

7:18

browser from our back end let's get to

7:21

showing our script results

7:23

so let's keep that as it is i'm going to

7:25

keep that there for you so you remember

7:27

the structure

7:28

how would you think that we would show

7:29

the results at a different endpoint

7:32

well once again i would use app i would

7:35

get a method so this time it's a get

7:37

request because we are not adding data

7:40

we're not editing data we're not

7:41

deleting data we're literally getting

7:44

data we want to get data when we visit a

7:46

certain endpoint and i'm going to pass

7:48

through the end point of this time let's

7:51

go with

7:52

results

7:53

okay so we've got our path

7:55

we've got a method we need the handler

7:58

so the handle is just a function i could

8:00

use a

8:01

arrow function if i wish it is totally

8:03

up to you

8:04

and this time

8:06

well i simply want to

8:09

show the results

8:10

so instead

8:12

of console logging out articles i can

8:15

actually move this entire thing so i'm

8:17

going to just cut this

8:19

i'm going to put the whole thing in here

8:21

so at the moment this is console logging

8:23

out articles we don't want to

8:25

console.log anymore we're going to show

8:26

this in our browser so again i need to

8:29

pass through the request

8:31

and the response it must be in that

8:33

order and i'm going to use

8:38

response

8:39

json and pass through the articles so we

8:43

don't need this anymore

8:45

and now if we visit the endpoint results

8:49

we should see all the articles so let's

8:53

go ahead here and i'm just going to do

8:55

forward slash results

8:58

and there we go that is how you get data

9:02

from your backend or in other words

9:03

server showing up in your browser now

9:06

this is useful for many reasons one

9:08

being that you can now make requests to

9:10

it from a front end that you have built

9:13

and use the data to build out projects

9:16

or two you can deploy this onto the

9:17

internet and have other uses for your

9:19

data think along the lines of building a

9:21

restful api

9:23

if you want to learn how to use a

9:25

restful api please watch my video on

9:28

this in the description below otherwise

9:30

let's carry on and i will show you how

9:32

to get the data that we have here into a

9:35

front end

9:36

so for this i'm simply going to create a

9:39

another file here is my server on the

9:42

same directory i'm going to create a new

9:45

directory called

9:46

source just to keep it separate from my

9:49

back end and in here i'm going to create

9:51

a new file called a app.js file where

9:55

we're going to build out our front end

9:57

and i'm also going to create a styles

10:00

css file in the source directory too

10:04

next i'm going to create an index.html

10:06

file in the root of my project so index

10:09

html like so

10:11

and in here i'm just going to put in

10:12

some boilerplate html

10:15

okay so here we have some boilerplate

10:17

html all i need to now do is link the

10:20

style sheet to our style sheet so i'm

10:23

going to go into the source directory

10:25

and i'm going to get the style css file

10:27

and i'm also going to link up this

10:28

script so once again i'm going into the

10:31

source directory and getting my app.js

10:33

file

10:34

okay so that is really it all of my html

10:38

is going to go here i'm not going to

10:40

make this super complicated i'm just

10:42

going to have a simple

10:44

div that i'm going to give the id of

10:48

feed so that we can put all our data in

10:51

here

10:52

okay

10:53

great so now if i go into my app.js file

10:56

let's pick out the feed so i'm going to

10:58

use this with the document

11:01

query selector this is a javascript

11:05

method that allows to pick out elements

11:07

based on their id

11:09

so i'm going to search for the id

11:11

for ids i need a hash

11:15

and then i'm picking out the feed so

11:18

once again this is going into my html

11:21

file and picking out the element with

11:23

the id of feed

11:25

the hash is for feed and then let's save

11:29

this as something i'm going to call this

11:30

as feed

11:32

display

11:34

just like so

11:37

you can of course use get element by id

11:39

instead i've just chosen to go with

11:41

query selector on this occasion

11:44

great

11:45

so now i'm going to have to get the data

11:47

from our backend or in other words

11:50

localhost 8000 forward slash results

11:53

into my front end so i'm going to do so

11:55

with some asynchronous javascript

11:58

so to do this i'm going to use the fetch

12:00

api i can use the fetch api easily with

12:03

javascript and this is the syntax for

12:06

doing so

12:07

the fetch api essentially provides a

12:10

javascript interface for accessing and

12:12

manipulating parts of the http pipeline

12:14

such as requests and responses

12:17

and the general syntax looks like this

12:21

so i'm going to now get the url that i

12:23

want to fetch data from and replace it

12:26

with this one right here and then this

12:29

is chaining that we talked about earlier

12:31

if you haven't used chaining before then

12:33

please do watch a tutorial on this in my

12:35

asynchronous javascript series what

12:37

essentially is happening is we are

12:39

fetching the results so the json from

12:42

our results page that we just created

12:45

and once that returns then we get the

12:48

response and we resolve this promise

12:51

that comes with that

12:53

and once that returns so our promise is

12:55

resolved then we do the next step and

12:58

then we do the next step okay so let's

13:01

go ahead and have a look at that i'm

13:02

just going to save this and i'm going to

13:05

open up this page in our browser

13:08

so open in browser

13:11

chrome

13:14

and inspect the page

13:16

you will see this message show up access

13:18

to fetch our backend has been blocked by

13:20

the cause policy so now we need to fix

13:23

this this is an easy fix for this all

13:26

you need is the cause package in your

13:28

back end so go ahead and gravitate to

13:31

your back end right here

13:33

and then simply run npm i for install

13:36

course

13:37

cores is a node.js package providing a

13:39

connect express middleware that can be

13:41

used to enable calls with various

13:43

options

13:44

it is a message that you might come

13:45

across quite often as a developer so if

13:47

you ever see this just know that it's

13:49

easy to solve by installing this package

13:52

right here

13:53

and once that has downloaded all we're

13:56

going to do is use the package in our

13:57

back end so i'm going to simply do

14:01

const cause require course

14:04

and then using app use again this is

14:06

something that express comes with i'm

14:08

going to pass through calls and call it

14:11

just like so

14:13

great so now if we look in our browser

14:15

there is our array full of objects we

14:18

are getting our data from the backend in

14:21

our front end amazing so we are nearly

14:24

there here is of course all of our data

14:28

there we go

14:29

now let's go back to our front end so

14:31

now we need to focus on how to get the

14:33

data from the console log area into

14:38

actual elements that display on our

14:40

browser

14:41

well to do this this is simple instead

14:43

of console logging the data i'm going to

14:46

open this out

14:47

just like so

14:50

and then because we know this is an

14:51

array i'm going to use the for each

14:54

method on the data coming back to us

14:57

so i'm going to grab the data and use

14:59

for each and for each of the items in

15:02

our array which i'm going to choose to

15:05

call article you can call this whatever

15:07

you wish

15:08

well i'm going to define a title

15:11

so const title and using this syntax i'm

15:14

actually going to

15:16

create elements

15:17

so let's create an h3 tag and then i

15:21

need to get the title from my

15:25

articles so let's have a look at the

15:27

response again it looks like this

15:30

so it's each article's title that i need

15:33

to put in here so i'm just going to put

15:35

the word article in front of here i'm

15:36

grabbing each item from my array and

15:40

each item is an object and that object

15:42

has a title so this is how we would grab

15:45

the title and put it in an h3 tag next

15:49

i'm going to actually have to inject

15:50

this into the div that we gave the id of

15:53

feed so we have to find this as feed

15:56

display and the first line of our code

15:58

so i'm just going to use feed display

16:01

and use the insert adjacent html

16:03

javascript method to put in the title

16:07

into that div i want to put it before

16:09

the end so i'm just simply going to pass

16:12

through the string before end and then

16:14

the title like so so the title we just

16:17

created

16:18

and let's refresh

16:20

and wonderful we are seeing all the

16:22

titles of all our articles showing up in

16:25

the browser

16:26

so hopefully you get how to do this i'm

16:28

just going to show you how i would do it

16:30

if i wanted to put the url in as well

16:32

so i would do this by changing title

16:35

let's maybe change this to article item

16:39

and then instead of just passing through

16:41

the h3 tag i'm actually going to wrap

16:43

the h3 tag in a div

16:46

just like so and i'm going to put

16:48

through a p tag for paragraph and i'm

16:51

going to also put the article url that

16:54

is returning back to me as part of the

16:56

object

16:57

okay so i'm going to put through a

16:59

article title nh3 tags and the article

17:02

url in between p tags i'm going to wrap

17:05

it in a div and that is essentially what

17:07

i am saying is my article item that i am

17:10

then putting into the feed display using

17:13

the javascript method of insert adjacent

17:16

html the insert adjacent html method of

17:20

the element interface passes the

17:22

specific text as html or xml and inserts

17:26

the results nodes into the dom tree at a

17:28

specific position okay so we are

17:30

essentially getting this and turning it

17:33

into html so we can put it into our html

17:36

file and once again the before end

17:39

simply means that we are putting it in

17:41

after its last child and what does this

17:44

look like

17:46

so this is what it looks like we are

17:48

getting all the data showing up here of

17:51

course if you want to style this out

17:52

please do grab the h3 tag and grab the p

17:56

tag and style them up using your style

17:58

sheet i however i'm happy with how this

18:00

is once again this is not a styling

18:03

tutorial this is one for learning how to

18:05

get data from your backend to your front

18:09

okay

18:10

wonderful

No comments:

Post a Comment