So now did we resist or some roots.
Let's add some logic for these roots.
For that I'll add a controllers folder.
That's not a must do.
But I want to split my logic here into the controllers and the routes into the routes folder and in
controllers.
All they had to do is start t s file and that will now hold the controller functions we want to point
at from inside our routes.
So in here I need a function which will export and I'll all created as a Erol function which is not
a must do though and all named is create to do.
And it is a function which will receive these typical parameters you get an every middleware function
and express a request a response and the next function.
And we just saw where we can get the types from we can import request response and next function from
Express at this imports as those types we can then add them here.
Now always adding all these types here as we did it in the error handling middleware as well can be
cumbersome though it works but it can be annoying.
So there is a shortcut instead of setting the types on all these parameters.
We can all just tell typescript which kind of function we want to store in this create to do constant
we can then simply import the request handler type from Express and then we set this here create to
do to type request handler.
And now you see types knows that here we got a request here we got a response and here we get this next
function because a request handler is defined to look like this.
It is a function with free parameters where the first parameter is the request.
The second parameters the response and the third parameter is the next function.
So this can simplify this setup here requests handler of course also is available because we installed
the types for Express and therefore we can add this import here.
Be aware that this import will always be gone.
Once this compiles to JavaScript because we're just importing a type here this is not importing any
code that would execute it's just importing a type and data for during compilation.
It will be removed because it doesn't add anything that would be required when this runs in regular
javascript with that.
However I got to create to do function and now why don't we add a two dos array here to manage some
to dos in memory only of course this will be gone whenever our server restarts because it's just a constant
in JavaScript in the end you could of course imagine talking to a database here as well but I want to
keep this simple because it's about JavaScript and TypeScript and not so much about database connections
so here.
I got to dos and now you don't want to create a new to do here.
When we got an incoming request and that should be a new object and why don't we create a blueprint
for it as object a model.
So I'll add a model's folder and in that models folder I'll add a dot to do dot t is file an export
class here which will name to do where I define how to do should look like there are we can add a constructor
and all we used is typescript shortcut where we can add an axis modifier like public and then a property
name like I.D. which is of type String let's say and text which is all of type String and then types
will automatically add fields properties with equal names and assigned values who pass to the constructor
to those fields whenever we instantiate debt to do class.
So I'll use this index here and you'll learn about this syntax earlier in the course already and now
introduced yes here in the controller we can of course import to do from going up when level models
to do and use this type and class.
Now here in our file and it's both a type and a class because well obviously it's a class and every
class automatically also acts as a type.
That's also what you solve for this course.
So now here we can first of all say that to do this here will be an array of to do is this extra annotation
doesn't hurt.
So did we get proper support.
Once we start adding to DOS or start working with the dos in this array and here when I create a new
to do I now created by instantiating this class and we have to pass an idea and here I'll create a random
pseudo unique idea with moth random to string.
This is not really unique but good enough here and now for the value here there.
I of course want to set some text which I expect to get in my incoming request body.
So I expect to get my text here basically on request body dot text so that I can then set text as a
value here.
Notice works but please see that this only works because typescript sees that this is of type any It
does not notice this is a string and it can't know because how would typescript know which data will
be part of our incoming requests.
It's not clear to it.
We're never stating anywhere what will be instead of the requests we receive.
Now how can we improve this.
Well we can use typecasting for example typecasting is always a great solution if you know about a specific
type but type doesn't.
So here on request body before we access text we can typecast this to be an object where we have a text
property which is a string because we we are building this app we know what we expect to be on our incoming
requests so it isn't.
We know that there will be a text property on the incoming request body we can cost it to be such an
object and now here we get proper TypeScript and also I.T. support.
So with that we add new to do.
Now we can reach out to to dos and pushed in you to do onto this array.
And again this will of course only store it here.
This constant and therefore only in memory because working with a database is not really directly related
to node or typescript but well to the database.
So with that we got to create to do function here.
Now let's go to the to dos roots and import this controller function.
So import from the controllers folder from the to dos file in there and import that create to do function
and point at this function here on the post root so that any request which targets slash to dos slash
nothing reaches de create to do function don't execute it here.
Just point at it it will be executed for you by node and express in your running web server later.
So now we get this function here.
We're not done though in the controller.
We all just should return our response.
So here we can send a response for example in Jason format with the Jason method where we say message
created dad to do or whatever you want and we have created to do Qi maybe where we forewarned you to
do that was added on the server side so that on the client we could also use that and maybe also set
a status code of 200 one year to indicate that we were successful and that a resource was created.
Now we just have to make sure that the body we're trying to extract here actually exists.
And for Dad an apt yes we need to parse de bodies of incoming requests for that we can import from the
body parts or package we old installed and from that package we can import the Jason method like this
and then here you can register dis execute it as a middleware.
So does this middleware provide by a third party package which will pass the bodies of all incoming
requests and extract any Jason data it finds in there to then populate the body key on this request
object with that past Jason data.
That's what this Jason middleware from the body parts or package does and with that we should have a
server that is capable of creating new to do is save all files and ensure that a recompile is without
errors.
Make sure node man is still serving the aptly as file and now we need to send a post request and with
the browser we can't do that but we can use a tool for that postman you can simply search for it and
then install the postman app on your system it's free to use once you got it installed you can start
it and you should see a screen that looks something like this if you see a modal overlay you can simply
close that and here you can now create a new request by clicking on that plus choosing the request type
in this case a post request entering the you are L here it's local hosted three thousand three thousand
because that's the port I chose here and then we need to send the request to slash to dos slash nothing.
So here to slash tattoos slash nothing.
We want to attach a body to the request and that should be a raw body.
Jason here to send some Jason data and now what do we need to send here.
Well what we extract in the middleware functions though in this case we should have a text property
as part of our Jason data we're sending.
So yea let's add text as a name and then any text of your choice like finish the course.
And if you now click send this sends the request and here's the response and this looks good.
We got the created to do back we see the I.D. It was generated and the text.
So our back and clearly works here and we wrote it all with typescript but we're serving it as regular
javascript that's the entire trick.