A recent development project of the company, the back end uses Nodejs. These two days need to be packaged to the customer demonstration, let the company a small guy to the previous 3D room packaging tools transplant. After packaging, found in the development environment to run a good project, can not access the. Problems that appear on the first page of the project cannot be accessed:
can not get file index.html
#express. Static
Where is the problem?
Nodejs back end of the express,index.html is a static file. We know that with Express built-in express.static you can easily host static files, examples, CSS, JavaScript files, and more.
Passing the directory where the static resource file resides as a parameter to the Express.static middleware provides access to the static resource file. For example, suppose you put a picture, CSS, and JavaScript file in the public directory, you can use the following code:
app.use(express.static(‘public‘));
So, find the code in the project, see where the static call is, and the same line of code:
app.use(express.static(‘public‘));
In this way, I have found the problem, I tell the small partner, this place does not need relative path can solve this problem. Due to the time limit of packing, I let the small partners to deal with the first simple, after finishing the package, in order to organize the following ideas:
app.use(express.static(‘resource/public‘));
Of course, the most important thing is that the problem is not difficult, their own more research, it is easy to find the problem, it will not come out of this problem, so the small partners themselves hit the palm.
Well, you're right, this place is still relative to the catalogue. Subsequent products will be changed to a better situation.
#express. Static method parsing
In fact, if the Express.static method passes in a relative path, express will convert it to an absolute path by itself, we can view the source code, and find the following code in Express.js:
exports.static = require(‘serve-static‘);
Description static Call Serve-static This package, directly find this package, view index.js, you can see the code, listed below the important two lines
...var resolve = require(‘path‘).resolve...opts.root = resolve(root)...
These two lines are, express to the relative directory into the absolute directory of code, you can see that the end of the path of the built-in object of the Resolve method, continue to look down.
#path对象的resolve方法
See the API documentation for this method directly, as follows:
Https://nodejs.org/api/path.html#path_path_resolve_paths
The following is an explanation of this method:
The Path.resolve () method resolves a sequence of paths or path segments into an absolute path.
What do you mean? This is the way to organize a series of paths or path segments into an absolute path, such as
path.resolve(‘/foo‘,‘bar‘);// return /foo/bar
For detailed instructions please refer to the documentation yourself, this place has a word to pay special attention to:
If after processing all given path segments an absolute path have not yet been generated, the current working directory is Used.
What it means is that if you finish all the path segments and do not generate an absolute path, you will use the current working directory (working directory). Like what:
path.resolve(‘bar‘);// 加上 /Users/terry 是当前工作目录, return /Users/terry/bar
A more complex example in the API documentation (note here resolve, right-to-left, refer to the documentation for more details):
path.resolve(‘wwwroot‘, ‘static_files/png/‘, ‘../gif/image.gif‘);// if the current working directory is /home/myself/node,// this returns ‘/home/myself/node/wwwroot/static_files/gif/image.gif‘
Now the question is, what is the current working directory.
Nodejs Current working Directory working directory
Nodejs the current working directory is the directory where node is started. That is, in which directory to start node, which directory is returned.
Note that this directory does not refer to the directory where the JS file resides
The current working directory can be obtained through the PROCESS.CWD () method.
The following is an example of the current working directory, if you write a JS file in the/users/terry/documents/jsworkspace directory, test.js, the code is only one line:
console.log(process.cwd());
At this point, if the command is executed under directory/users/terry/documents/jsworkspace: The node test.js output is as follows:
/Users/terry/Documents/JSWorkspace
But if the command is executed under directory/users/terry/documents/: node./jsworkspace/test.js, the result of the output is:
/Users/terry/Documents
As a result, you can see that you are executing the node command in that directory, which is the current directory.
Back to the previous packaging problem, because in the development phase, is generally directly in the JS file in the directory where the node command is executed, so the relative directory is written relative to the current JS file directory is not a problem.
But after packing, node's execution was placed on the upper level of the JS directory. At this time relative to the directory "public" is not relative to the relative directory of the JS file, but compared to the previous layer, the natural can not find this folder, and thus can not find the folder under the Index.html file.
#如何解决
The workaround:
- As already mentioned in the previous, change this relative directory. But this approach is lame. Because the directory that starts the node command may change, but if it should, the node command execution in the development phase also needs to be changed. It's not a good way to be compatible anyway.
- Use absolute paths directly. But this absolute path on different machines is not the same, how to solve it? You can consider using global variable dirname.
#全局变量dirname
View API Documentation Https://nodejs.org/api/modules.html#modules_dirname
See the explanations below:
The directory name of the current module. This is the same as the? path.dirname()
? Of the? __filename
.
What do you mean, return to the directory where the Nodejs JS file is in time.
With this variable, we can solve the problem with the following code.app.use(express.static(__dirname + ‘/public‘));
This article in the public number good synchronous release, readers can also pay attention to the public number:
The path problem of Nodejs