When creating an express.js project of any kind the first thing that I work with is the express top level function. It is the function that is exported when grabbing at express with require when making the typical app.js file. This function is used to create instances of an app object, and it also has some additional methods attached to it as well.
This post will quickly cover some of the most important things to know about with the express.js top level function, but I will not be getting into any of them to an extreme level of detail. In othe words this post acts as an index of sorts for other content that is of greater value. I have my main mega post on express that acts as the main index of all content on express that might be a good starting point on this site for all things express.
Any express object will often involve calling the top level function that is exported from the express module to create an instance of the app object.
It is also possible to create an express app middle ware by using express.Router as well, more on that later though. Here I am just giving a simple hello world example of the app object, but there is much more to know on it. If you are wondering on how to go about parsing the body of an incoming post request for example you will want to use app.post, and also a body parser as well.
You can read more on the app object.
This is just a basic example that should work at serving up a public folder in the same folder as the script. I will not get into express static in detail here, as this post is more of an overview of everything to be aware of in just the top level function.
However you can read more on express.static here if interested.
If you are not aware of routers yet now might be a good idea to give them a try. They are a great way of helping to break your project down into smaller components that can then be used in the main app.js file with app.use.
for an example I could make a router.js file like this:
and then use it in a main app.js file like this:
Be sure to read more about them in my post on express.Router
I hope this post has given you a good overview of the express.js top level function. If interested in other express.js related content, you should check out the main express.js mega post that acts as a central index of all express.js content on this site. It is kind of like this post, only it is the main index of sorts.