What does static('public') do?

Question

What does static('public') do?

Answer

The static method of express allows us to serve all the static files of our application, that is to say, all HTML, CSS, and JS files that will comprise the front end visuals on the browser. In our case:

express.static('public');

Public is the name of the directory where we find those files, for example, in our execise:
27%20AM
We can see that public contains a CSS and a JS directory, as well as an index.html and the expressions.js file.

By placing the express.static() in app.use, we let our server know that we will be rendering that index.html file and implementing all those static CSS and JS files related to it.

app.use(express.static('public'));

That is the purpose of the static() method.

8 Likes