So let's get started.
As I mentioned here I will create a very basic REST API.
But of course he could build any node application with or without express and in any kind of way with
typescript.
So this is just an example.
You're not restricted to this specific application in the source folder or ETA app.
Yes.
File which will be the root entry point where we spin up that server and there I want to import something
from Express from this Express framework.
Now you probably know how we can add express here and set up a web server with Express we import express
with this syntax here and then here we can execute express as a function we get back up to app object
on which we can call listen to listen on a certain port for incoming requests.
Now this would work in normal javascript but you already see I'm getting an error here now.
I also get a suggestion why I might be getting this error here because require actually is a function
that exists in node J.S. but not in the browser side.
Now in t is conflict of Jason File.
I'm not using any special lips that would support.
No it's J.S. so indeed typescript is not aware of this general function which exists in a notorious
environment.
Now to make this work we need to inspect all extra type with npm install dash dash save dash death we
can install at types node to install all the types we need to work with node j us.
So let's run this command and see if that helps us.
Yeah it's done and does error is gone but maybe you also saw that on app I didn't get any auto completion
it also doesn't error out but if we hover over app we see that typescript doesn't know what's in there.
It's of type any.
So we probably should install more types and also install at types slash express to install express
specific types.
And that's a pattern you already know we had a look at that and a third party libraries section and
of course there I always showed you how you can install types packages to get better support in your
typescript scripts.
So with this type added without any extra configuration huh.
It still doesn't work.
Well the reason for that is to import syntax we're using here.
This is the regular common js import syntax which notorious uses by default.
But here we're actually in an environment in typescript where we can use a different import syntax we
can use the same imports index we know from E as modules in the browser so we can use import Express
from Express for example.
And if we do that and then hover over app you see now it's off type Express.
So now we got types support.
Now one important note in note J.S. There also exists a syntax which is similar to this one.
Now it's under an experimental flag and it's been under that flag for a very long time.
So using common J.S. and no chance is by far the default and most common way of adding imports and exports
are a syntax looking like this one is supported if you set this experimental flag but here in this code
we're actually not using this experimental note J as syntax.
Instead we're using typescript once we compiled it to JavaScript this will change and I can actually
show this to you so that we can also execute this if we run t us see here maybe TSC dash W to already
enter watch mode you see this starts compilation it creates a dist folder and in there we now have an
app J.S. file and in here this now looks different.
You see there it now doesn't set up that import the way we have it here it look it uses a different
syntax instead.
So here does this code which actually will get executed and we can execute this with node one with this
tool that automatically restarts the server if our code changes by opening a new tab here in the terminal
keep the other tab open because that's our typescript compilation process and in that new tab we want
to start our server with the help of node Mun for that let's go to the package dot Jason File and add
a new script a start script maybe where we say node mourn this slash app J.S. so that node 1 executes
this file in the dist folder and starts our server with that.
And with that added and the package not Jason files saved we can run NPM start to speed up our development
server and now it is serving this this aptly as file now that's the general setup we have and that's
typescript already doing some work here we also have type support and here you see an app.
Yes we can write code that looks very familiar to us with the import export syntax we already know but
what we get as an output is in the end regular javascript node J.S. code and that is extremely useful.
Now with that let's do more useful things than just starting up a server that does nothing.