Day 89 Node.js Express Framework

Finally, the basic understanding of Node.js is understood, so its time to use a framework in it. Express is a web application framework designed for building web applications and APIs. It is also the common use case for Node.js.

Installing express on node is quite simple.

I just had to create a variable equal to requiring express from Node. Then I just called express to run it. The next issue I would run into is where is the file listening. If you have the file listening to a specific server it may mess things up when you switch it from test environment to the deployment server.

A method to overcome this problem is to create environment variables. Environment variables are global variables specific to the environment server our code is living in. Different servers can have different variable settings, and we can access those values in code.

HTTP method specifies the type of action the request wishes to make. GET, POST, DELETE, and others.

The method above gets an html file but a more common use case would be to get JSON files.

Another reason why Express is so commonly used is because of its middleware capabilities. Middleware is code that sits between two layers of the software. In the case of Express, sitting between the request and the response. This allows requests to be automated responded to when files are being requested from the server. HTML, CSS, and image files are ‘static’ files. Express.static is a middleware function that serves static files.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: