Now to get started using Express,
I simply want to migrate this existing NodeJS code here
to Express.js code,
since that will help us understand
how Express.js works
and how it can make our life a bit easier.
And for that, instead of requiring HTTP,
this core NodeJS module,
which we use for building our server,
we will now get rid of that
and instead require Express,
this Express package that we installed.
You can require any package that you installed
or the core packages like HTTP,
which were built right into Node.
Here we will now require Express,
since we did install that package.
You can require any package
that is part of your dependencies
or a dependency of another dependency.
Now just as before,
this will give us an Express object or something like that.
And we can store this in a constant.
But actually I said something like that
because it turns out that this Express thing,
which we require here,
so the main thing made available by the Express package
is not an object,
as it was the case for the built-in HTTP package,
but instead it's a function.
Packages can simply decide what they want to expose:
an object, a simple value like a number, or a function.
And here it's a function,
hence we can execute Express like that.
The result of that will be an object though,
a so-called app object.
The name is up to you,
but when we work with Express,
we typically call the overall project
an Express application.
And by calling Express, we get an app object,
which will then unlock more functionalities for us.
Because on this app object,
we can now call listen,
just as we did it before on that server
which we created manually,
but now that server is created
behind-the-scenes for us by Express.
Through listen, we can still pass our port number,
3,000 for example.
But with that we already have a shorter way
of creating such a server then we had before
where we had to call create server.
Now, still we want to let Express,
and therefore implicitly Node,
know what should happen for different requests
that might be sent to different paths on our URL.
And for that we can use this app object
after it was created, before we listen typically,
and we can call another method on it, besides listen.
And that is the get method.
A get will allow us to define a request handler
for incoming get requests.
You might recall that in the forms section,
I briefly talked about get and post requests,
but there we weren't able to do anything with post requests.
And a get request is the default kind of request
that is sent by a browser
if you just enter a URL
and hit enter in the browser address bar.
Therefore here I want to handle get requests
sent to a certain path,
so the part after our domain and port.
And here, I want to handle get requests
that are sent to slash current time.
So that will handle requests
that will be sent to local host 3,000 current time,
that's the idea.
And arguably that's shorter
than having to manually write that if statement like this,
where you check the incoming request URL.
Now what's missing is the function
that should be executed for that incoming request.
And that's simply the second parameter value
that you can add to this get method.
The first value sets the path
for which you wanna filter basically.
The second value is the function
that should be executed for such incoming requests.
And here we can, again, create a function,
handle current time request could be a fitting name,
or we use an even shorter notation,
which is not Express specific,
but can be used in JavaScript in general.
We can create a so-called anonymous function like this.
So I'm creating a function right in the place
where it's needed, but it's a function without a name.
Instead, we have the parentheses
directly after the function keyword.
We could have done this before as well.
Here for create server,
which also wanted a pointer at a function,
we could have also created the function just in time
here in this place.
This still doesn't execute the function immediately,
it just creates it and then passes a pointer
to the created function,
to this function to which you pass it,
so create server in this case.
I didn't do that before,
but I will do it now with Express.
I just wanna emphasize that it's not Express specific.
In this function,
you can now define the code that should be executed
if we got an incoming get requests to this path.
And here, I want to send back a response
with that status code and with that h1 element inside of it
that contains this date string here,
this date snapshot.
For that function,
just like the handle request function we defined before,
gets two main parameters passed in automatically by Express.
And that, again, is a request parameter value
giving us more information about the incoming request,
and a response parameter value,
giving us functionalities to prepare a response.
Now, very often we abbreviate that with req and res,
but that's optional.
It's a bit shorter though and hence I'll use that.
Actually, we also get a third value here
passed in by Express
and that's a function which we typically call next here.
So it's a function we can execute inside of this function.
For the moment though, we don't need it
and hence all omit it here.
I just wanted to briefly mention it already.
But now we get information about the incoming request
and we get an object with functionalities
to prepare a response.
Therefore, if I now want to send back a response,
which is what he did before as well, with that h1 element,
we can use this response object here
and there just call send.
So not end like before but send.
Because this is not the same response object.
We also don't have the same request object.
Instead, we got request and response objects
created by Express, by that third party library.
Therefore they have properties and methods
defined by the authors of that library.
And response happens to have a send method,
which does what the name implies,
it sends back a response.
And in its simplest form, you can pass the data
that you want to send back as a parameter value to send.
So basically just as we did it for end before.
Hence I'll grab that value from down there
and pass that to send then at a semi-colon here.
Now, before we test this,
let's also handle the just slash case here
so that the request is just sent
to our domain slash nothing.
For this, we now don't add any if check here,
instead we just define another so-called route
by again using app get forward slash nothing.
And then again, we have our handler function,
which gets the request and response objects,
and in there we define our code.
So in here,
I now wanna send back this hello world text here,
so I will write res dot send hello world.
And that will then handle the requests
to a local host 3,000 slash nothing.
As a side note, I'm never setting the status code here
because Express will set it to 200 as a default
if I don't set it myself.
We could override it,
but we don't need to do that right now.
And hence this is now how we can handle
these two different requests
that could arrive here on our server.
And therefore now we can get rid of
that old NodeJS code down there
because now we're using Express instead.
And if you now have a look at this code
and you compare it to the NodeJS code from before
then this definitely looks more structured
and is a bit easier to digest, read, and maintain.
And for this simple project, that might not matter too much,
but if you're building more complex projects,
you can hopefully see the value Express might offer to you
since it makes writing and structuring code easier
than it would be with just NodeJS.
And that's just the tip of the iceberg,
especially once it comes to parsing and handling user input,
Express.js will make it way easier than just NodeJS does.
So therefore now, if you save that file,
we can again run our app.js file with Node.
That hasn't changed,
it's still Node app.js.
And this will then start this web server
as it did before.
But if we now enter local host 3,000 like that,
we see hello world.
And if I enter slash current time, like we did it before,
I get this current timestamp.
And of course that's a different timestamp
every time I reload here,
because this is generated just in time on the server.
That was the case before as well
but now with Express.js,
our code is a bit easier and more structured.