[Solved] What is this express variable is doing?


Line 1: You import the express node module that you installed with npm i express and store it in a constant (const).

Line 2: You import the function or variable createReadStream from the file system module of node.js (fs module) and make it available for use in this file.

Line 3 you assign the express() function from the express module above to a constant called app, so you now have everything express related available to you on the app constant.

Line 4-5: You use the get method from the express() function you have stored in the app constant, and create a route for the base url of your app / (e.g. domain.com/ or localhost:8000/). If you request something from the server you send a GET request. If you send some data in you POST or PUT, for example, so the express() function in app have these methods for you to use, too (app.post for example).

When Postman or a regular user with a browser hits this part of your domain (route) with a GET request, the arrow function on line 4 (req, res) => kicks in. It takes in the request (req) and the result (res) parameters so you can use those inside of the function if you wish. On the req parameter you have available whatever is in the body the user sends in from a form, for example. In your case your route streams back a html file to the user via http in order to display it in the user’s browser.

0

solved What is this express variable is doing?