Node.js is a very popular JavaScript server-side running environment. It allows developers to use JavaScript for server-side programming. Compared with traditional server-side languages, such as PHP, Java, etc., Node.js is more efficient and It is lightweight and easy to use, so it is very popular in the field of web development. This article will introduce how to specify paths in Node.js, including relative paths and absolute paths.
1. Relative path
Relative path refers to the path relative to the directory where the current file is located. Relative paths are usually used to access files or directories.
For example, we have the following directory structure:
project ├── server │ └── app.js ├── public │ ├── index.html │ ├── js │ │ ├── main.js │ │ ├── lib │ │ │ └── jquery.js │ ├── css │ │ └── style.css │ └── images │ ├── logo.png │ └── icon.png ├── README.md └── package.json
Suppose we want to access the index.html file in the public directory in app.js, we can use relative paths to access:
const path = require('path'); const fs = require('fs'); const indexPath = path.join(__dirname, '../public/index.html'); fs.readFile(indexPath, (err, data) => { if (err) throw err; console.log(data.toString()); });
In the above code, __dirname represents the directory where the current file is located, '../public/index.html' represents the directory one level above the directory where the current file is located (that is, the project directory), and then enters the public directory, and finally finds the index .html files.
It should be noted that when using relative paths, you must pay great attention to your location. If you make a mistake, the file or directory may not be found.
2. Absolute path
The absolute path refers to the complete path starting from the root path and is not affected by the directory where the current file is located. Using absolute paths makes it easier to access files and directories.
Node.js provides the path module to handle path problems. We can use the path.join method to splice the paths together to generate an absolute path.
For example, in the above example, we can use the following code to get the absolute path of the public directory:
const path = require('path'); const publicPath = path.join(__dirname, '../public'); console.log(publicPath);
In the above code, __dirname represents the directory where the current file is located, '../public ' means to get the absolute path of the public directory from the directory one level above the directory where the current file is located (that is, the project directory), and then enter the public directory.
With an absolute path or relative path, you can access the required file or directory. To summarize:
It should be pointed out that in actual development applications, it is recommended to use absolute paths to reference files and directories, because this can more clearly indicate the location of the file or directory and reduce the error rate of the code.
The above is the detailed content of How to specify path in nodejs. For more information, please follow other related articles on the PHP Chinese website!