So we got this basic dummy server up and running but this server is not doing anything useful of course.
Now I said I want to build a very simple REST API and for that I will actually create a new sub folder
in the source folder which will name roots in there.
I will add a file that should handle a certain set of roots of this REST API.
And now it's up to you which kind of API you want to build.
I will build to do API here.
If you took do you react and typescript section.
That sounds familiar to you because dear we built a to do app too.
This is totally detached from that section though it will not work to gather with react of course you
could connect it though on your own.
But here I will build such a simple to do API simply so that we see how typescript works with node and
express the application we're building is not too complex but it will contain a lot of interesting concepts.
So here it will add my two to routes by adding a to dos dot t as file in the root folder and in there.
I now want to use Express J S to register some roots for Dad I'll import something from Express and
that something is now actually the router.
Now that common J S syntax where dad typically is that you import express by using require express and
then in a new line you off the C code where we do this here right now.
That's something you can do.
But here I'll just import like that now this gives us a router and we can simply call a router here
as a function and this allows us to register middleware.
So in the end routes and points for incoming requests where we then execute some logic upon these requests.
Now for the to do app I want to register some basic routes opposed rout at slash nothing where we can
add a new to do so here we'll need some code some function which then adds a to do somewhere for now
I'll just specify no code at all.
Just set up the route in general here so we have to post request then a get request to get all to dos
let's say then also let's say patch request to update it to do and dare I expect to get the idea.
Dynamic segment into you are L and then here also a delete request to delete a to do by I.D..
These are my routes and now we can export the configured router as a default in this file.
Now nothing will happen on these routes right.
I haven't set up any function that would execute when a request reaches these end points but we at least
now know which routes we want we'll add the functions in a second step later.
For now let's just connect the router here to configured routes to our running server here in Apt.
Yes and to do that we just import our let's say to do routes from dot slash routes to do this.
So we're importing that export router into DOS into apt.
Yes.
And now here we need to connect it to our running express application by using app use and then let's
say we want to forward all requests that start with Slash to dos to our two routes.
So requests targeting a different you or l will not be handled there.
Speaking of handling in the Express middleware set up here which we're using we can also set up a middleware
that will handle errors so any errors that might be thrown anywhere in our code and we do this with
app use and then we set up such a typical middleware function here where we get the request response
and a next function.
And again this all requires you to know some express J S and to know no J.S. I'll not dive into exactly
how middleware works and express and what Middleware is right.
So that's a regular middleware function here as you should know it from Express J.S. and you probably
also know that there is an alternative to debt function a function that takes for Ramírez where the
first parameter is a potential error and now it is is a error handling middleware function which will
be fired automatically by express.
If in any of our middleware prior to this one you have an error.
So this my error handling middleware function and you see we got a bunch of errors here bunch of errors
because this is all of type any.
And we're also not using all these values.
Let me make it clear which kind of types we have here.
This will be of type error.
This will be of type expressed on request or you simply import request here as an extra named import
from expressed is also works if you've got the Express types installed which we do then here we also
got the response and a next function here and then we can adjust our types here.
This is now of type request.
This is of type response and this is this next function which we can execute to let the request continue
its journey to the next middleware in line.
Now that's our error handling middleware.
It will fire whenever we have an error in one of our other Middleware is in the here we could send back
a response with let's say Jason data where we have a message which is error message and where we may
be also set error message I mean and where we may be also set a status code of 500.
And of course you can have a more elaborate error handling functionality than I do have it here.
But with that we added our roots we added our error handling middleware why don't we add some logic
to this API now in the next lecture.