Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does omitting the line app.use(express.static(__dirname, 'public')) stop my html pages from loading css files?

I'm learning to use NodeJS and Express and just used "express" to generate the scaffolding for a project. I don't understand the purpose of:

app.use(express.static(path.join(__dirname, 'public')));

My understanding of app.use() is that it loads functions as middleware, so app.use(express.static(path.join(__dirname, 'public'))) must be loading a function, right? Is this function loading paths to some files that I'm declaring as static?

In my layout.jade file, I have this line in the head:

link(rel='stylesheet', href='/stylesheets/style.css')

How does my app know to begin the href link with a '/public' (if that's what it's doing) when I set the app.use(express.static) line? Because when I get rid of the app.use(express.static) line, it gives a 404 to finding the css file, even when I change the href to '/public/stylesheets/style.css'.

And how does it know that I'm trying to access that static file at all? What if I had a file called 'root/stylesheets/style.css' as well as the 'root/public/stylesheets/style.css'?

like image 834
jarrad_obrien Avatar asked Dec 09 '15 08:12

jarrad_obrien


People also ask

What is Express static (__ Dirname?

app. use(express. static(__dirname + '/')); The static middleware handles serving up the content from a directory. In this case the 'root' directory is served up and any content (HTML, CSS, JavaScript) will be available.

What does app use Express static do?

use(express. static()) adds a middleware for serving static files to your Express app. You can use the express. static middleware to make it possible to access files from this folder via HTTP.

What are Express static files?

Static files are files that clients download as they are from the server. Create a new directory, public. Express, by default does not allow you to serve static files. You need to enable it using the following built-in middleware. app.

What is __ Dirname in node?

It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.


1 Answers

Right, app.use() loads a function to be used as middleware. In this context, it loads the result of express.static(path.join(__dirname, 'public')).

The result of express.static(path.join(__dirname, 'public')) is a function (in JavaScript, functions may return functions), a function that express understands as a middleware (i.e. it has the following signature: function(request, response, next) {

express.static() is a function that takes a path, and returns a middleware that serves all files in that path to /. (If you wanted to prefix it with /public or whatever, you'd write app.use('/public', express.static(path.join(__dirname, 'public'))), where the first /public is the web path and the second is the filesystem path of the files being served).


For better clarity, the following:

app.use('/a', express.static(path.join(__dirname, 'b')));

would serve all files inside of the b directory, and have them accessible through http://example.com/a/FILE.

like image 164
Madara's Ghost Avatar answered Oct 13 '22 14:10

Madara's Ghost