Understand the meaning of Express express.static and __direname and __firename

Understand the meaning of Express express.static and __direname and __firename

One: Understand app.use(express.static(__direname + '/public'));
Pass the directory where the static resource file is located as a parameter to the express.static middleware to provide access to the static resource file, such as placing it in the public directory With css, images, and javascript files, we can write it as above.
Of course, we can also write it as follows:
app.use(express.static(path.join(__dirname, 'public')));

First look at the directory structure of the project as follows:

### The directory structure is as follows:
demo # project name
|   |--- public    
|   | |---js
|   | | |-- index.js
|   | |--- index.css
|   | |--- index.html
|   |--- server.js                                      
|   |--- .gitignore

The server.js code is as follows:

var express = require('express');

var app = express ();

var path = require('path');

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

app.get('/', (req, res) => {
  res.send("aaa");
});

app.listen(3000, (req, res) => {
  console.log('app is running at port 3000');
});

Using the command line, after entering the root directory of the project, enter the command node server.js to start the server;

So now we can access the file like this;
http://127.0.0.1:3000/js/index.js
http://127.0.0.1:3000/index.css

If our static resources are placed in multiple directories, we can call the express.static middleware multiple times; for example:

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

If we want all files accessed through express.static to be stored in a "virtual" directory (that is, the directory does not exist at all),
we can do this by specifying a mount path for the static resource directory, as shown below :

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

So now accessing the resource file becomes as follows:

http://127.0.0.1:3000/static/index.css
http://127.0.0.1:3000/static/js/index.js

Two: Understand the __filename variable
Inside any file module, you can use the __filename variable to get the filename with the full path of the current module file.

For example, now add the following code to the server.js code:
var tt = require('./js/index.js');

Then add the following code to the js/index.js code:

console.log(__filename);

Continue to enter node server.js in the root directory of the project, and the full path file name will be displayed on the command line:

/Users/tugenhua/个人demo/node0420/express-static/public/js/index.js

Three: Understand that the __dirname variable
is inside any module file, and you can use the __direname variable to get the full absolute path of the directory where the current module file is located. The operation is the same as above, and continue to add the following code to index.js:
console.log(__dirname);
print the result as follows:

/Users/tugenhua/个人demo/node0420/express-static/public/js

demo in github

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348317&siteId=291194637