Now, let's dig a bit deeper into Node.JS.
We've got some basic Node.JS code here,
but of course this code isn't doing too much.
What I wanna do now is I wanna support a brand new path
in my URL.
To be precise,
I wanna make sure that if we enter our domain,
so in our case, localhost,
and then the port, /currenttime,
I wanna show a current timestamp on the page.
If we enter just localhost:3000.
So basically, localhost:3000/ nothing, you could say.
The slash doesn't really make a difference here.
Then, I instead want to show this Hello World!
or welcome page.
So I wanna send back two different kinds
of responses based on the path, as this part is called here,
that is entered in the URL.
Now, of course, we did already send back different responses
before in the course when we worked
with different HTML files.
Then, we were also able to enter slash
and then the HTML file name to load different HTML files.
But the whole idea of getting started with Node.JS is
that we see that we can now actually have full control
over the response that is being sent back
and that we no longer need to create HTML files
as responses, but that we can control the response
with Node.JS and JavaScript only.
And then, in the next sections,
we are going to reintroduce HTML files,
but then we'll also write them such
that they are a bit more dynamic, but more on that later.
So to handle these two different paths here
in this handler request function,
we now can extract some extra data from this request object,
which we get automatically.
This request object actually has a URL property.
It will have that URL property because this object,
which we get is created by Node.JS,
and it is created such that it has such a URL property.
And the URL property actually will not hold the full URL,
but it will hold this part.
So the part after our domain and port.
And therefore, we can now use this request.url property
in an if statement and check if request.url is equal
to /currenttime so that we then,
inside of this if statement,
can write the code that should execute
if a request is sent to that URL.
And else, if a request is sent to a different URL,
or maybe else if, if we wanna be more specific,
if request.url is equal to just /,
so just our domain with nothing after it,
that's represented by just /,
then I wanna execute different code.
And then, we could move this code here and move that
into this else-if branch and auto format this code.
And here I can still say Hello World!
But then, I'll also copy this and added here inside
of this first bronch.
And then, here, I can sent back a different piece
of information.
I might still want to create a h1 tag here,
but I actually wanna create it such
that I can insert some dynamic data into it,
because that's one of the main reasons
for using Node.JS, or for using a programming language
that executes on the remote machine.
I want to be able to send back different responses
that might contain dynamically fetched or generated data.
And that's what I wanna simulate here.
Between the opening and closing h1 tag here,
in this if branch, I want to output the current timestamp.
And I can do this by injecting something
between these two strings, a third string,
which is the current timestamp.
And in JavaScript, both on the browser side and in Node.JS,
we can get that current timestamp by using new Date,
which creates a date object.
And on this created new Date object,
we can call toISOString to convert this date object
into a string representation, so into some readable text.
toISOString is a method which we can call
on that created new Date object.
And new Date simply is a built-in way of creating
such a date object.
Now with that, if we save everything,
we have two different URLs we can target
and we'll get different responses.
So if we now open this terminal again,
and here I did that with the shortcut,
you can, of course, still also do it manually.
You can see the shortcut if you go to Preferences,
Keyboard Shortcuts.
Here, I'll then again execute this app.js file
with node app.js.
This process then keeps on running as learned.
And now, if I visit localhost:3000,
I still get Hello World!,
because that is basically localhost:3000/ nothing.
And that is this second branch here.
If I enter localhost:3000/currenttime though,
then I get this current time snapshot.
Of course, it's not formatted in a super pretty way,
but it's good enough for the moment
and it proves that this works.
If I enter anything else, like /something,
instead, we'll get an error.
This will keep on loading, and ultimately, it will crash,
because this function executes
but never produces a valid response.
And therefore, ultimately,
this request will time out because it never gets a response.
You might wanna handle such scenarios as well,
but here we'll just ignore it and focus on the two cases
that do work.