In one of the earlier sections on fundamentals we created an edge TTP So were using Express.
We're going to use the same approach here and create an HDTV server.
And since we have already seen how to create and as GTP said we're before just to speed up the process.
I'm simply going to paste in some code that I have ready with me.
All right let's walk through this code.
We are referencing a couple of NPM audience here.
EX-PRES is to create an STV server.
Compression is to enable compression and this is optional.
That is just to work with the directory path.
Body passer as we have seen earlier will allow us to parse the SCDP regressed in the GSM format.
And cars is to control the cross region resource sharing.
We also reference the App Store S-5 which will hold our rest API endpoints are the routes then we call
the Express function and then enable cars and compression we are using the day for cars configuration
and in a production environment.
You should provide more control cars.
All is with outdoor set muse.
We tell express the location of our SOS edged team efforts then we use the body parser middleware to
parse all incoming requests to Chaisson and then we tell express the directory from where to serve the
static pages of our app to the end users.
And we also tell express the location of our routes.
Finally we have some code to catch errors.
So for 3:56 our page not found errors.
This curve is simply going to render the index page.
This is because no application that we are building is going to work only as a arrest API.
So any SCDP requests to our arrest API will be handled by our app out fine.
And so all other as GDP requests likes in the actual front end to the end user etc will be handled by
our angular
said.
Express is simply going to render the index page far or not API grasps the next page is the starting
point of our Front-End angular.
Then in case of any server side errors we simply launch them to the console and also return the error
message as SCDP response.
Finally we export our app more ado so our server just fine can use it
in the server just fine.
I'm going to paste in the code that I have ready with me and then we'll work through it.
We referenced the TTP and debug models and then reference are at Modu as well.
We then sent the board to be asked to serve our application.
We look it up from the environment variables and if we don't find a value there which is the default
mode as 3000 then we create the server with SCDP God create server and pass our app reference.
And finally we ask the server to listen to incoming requests on the predefined or rest of the code it's
optional and useful for debugging purpose.
Then we have to create the API endpoints or routes that people from different operations on our dynamically
table on behalf of our application to do that.
Open the app Roadstar just fine
Briest we must reference the Express Madill and then declare Routier variable are constant as expressed
dot router
and then we can define our API functions using different methods available with this express router
line get hosed and so on.
We'll do that shortly in the next lecture we also have to export this routers are apt or just find can
use it just like that.
All right.
Before we can test we must install the NPM all use that we use here.
So NPM install dash dash save followed by names of all the modules we need that is EX-PRES edge vs compression
bad body parser
Carlos
as GDP and debug
Osten.
Now let's ram MP and start to spin out the server.
And we can see that server is now listening on port three thousand.
Let's open localhost goaland 3000 in our browser and we should see our index page with Hello World message
awesome.
Our server is ready and now we can create different routes for our rest API.
So let's do that in the very next lecture.