I am not sure what the method .use() does, the course says that it’s used to mount middleware (which then states that it will explain later what middleware is, so that isn’t much help). What does ‘mounting middleware mean’?
in “computer speak” the word mounted usually means ‘to make accessible’ in some way. For eg. you can mount a drive. Meaning that before it was mounted, you couldn’t access it, and after you mounted it, it became available for you to use.
So imagine that ‘middleware’ is some kind of program or programs and when you call use you are telling the server to make that software available (where it was not before).
Another question. What is the difference between these two lines of code? I read that the first one points to the /public route, but what does that mean?
The first one exposes the /public files if the route to your site is via /public (like https://mywebsite/public)
(see Randell’s response below instead)
The second one exposes the files in the root path (so the path just above the public folder) if the route to your site is via /public
If you have a folder setup like:
-rwxr--r-- 4 hbar1st hbar1st 60 Dec 20 00:06 aRootFile
drwxr-xr-x 4 hbar1st hbar1st 4096 Dec 20 00:06 public
The file aRootFile will become readable thru the 2nd line of code.
the directory public and its files will become readable through the 1st line of code.
Thank you for your patience and responses, but I am still lost. What do you mean the first exposes the /public files? I tried to access https://boilerplate-express-4.ericdominguez.repl.co/public and nothing came up.
The first one allows visitors to access a route at yourwebsite.com/public. The files for this route are located on the server in a folder at the __dirname + '/public'
This means if you have a file named index.html located on the server at __dirname + '/public', a visitor would access the file at yourwebsite.com/public/index.html.
The second one allows visitors to access files at the root of your yourwebsite.com even though the files are stored on the server at __dirname + '/public'.
This means if you have a file named index.html located on the server at __dirname + '/public', a visitor would access the file at yourwebsite.com/index.html.
Is there a preferred way to mount those middlewares?
What can I google to learn more about this topic? I’m still confused. It seems like its teaching me something that I could just fix with one way of writing middleware.
It depends on the app and how you want to structure the files for it. If you have a simple site that will not have different routes, then you could go with option 2. This will allow visitors to access files (i.e. index.html, someother.html, etc.) from the root of the site while have the actual files in a different folder than the root folder on the server.
Just keep going in the challenges for this curriculum section and you will see what is typically done.
Most of the time, you will probably go with the first option to keep static files (more like css or js files) accessible in a different public folder. Everyone has different approaches to this though. There is no right or wrong way and really just depends on the app being built.