Introduction to the
Front-end engineers need to learn NodeJS to expand their knowledge boundaries, but also make themselves more convenient to understand Webpack, front-end engineering, etc.
1. process
The Process object is a global variable in Node that provides information and control over the NodeJS process and is always directly available to process as a global variable.
Argv is a common attribute. Process. argv returns an array of values entered on the command line when node code is run. For example, if we run a piece of Node code and add some parameters to the command line: Argv = [‘node’, ‘/path/to/your/app.js’, ‘xiaoming’, 2]
This property value is relatively common and is used in the Webpack configuration in VUE-CLI2. Load the different WebPack configurations based on the following parameters to determine whether the development or package, test, and so on environment is currently started. Interested in vUE webpack configuration code to view.
2. Fs file system
Node as a back-end program, I/O processing capability is naturally one of its core capabilities. The FS module provides an API to interact with the file system. All file operations are synchronous and asynchronous. Const fs = require(‘fs’); .
2.1 readFile and readFileSync
Fs.readfile (‘/path/to/file/’,()=>{}) and fs.readfilesync (‘/path/to/file/’) are both asynchronous (non-blocking) and synchronous (blocking) methods of fs operating on file systems respectively. Any method in Node that ends in sync is a synchronous (blocking) operation.
Introduction to Usage:
const fs = require('fs');
// 1. Read the file asynchronously. The data read is in its callback function
fs.readFile('/path/to/file', (err, res) => {
const result = ' ';
// res reads the file contents asynchronously. The default format is buffer
// Add a status listener to get the res file
res.on('data',(data) => {
result += data;
});
// Monitor the end state of the res, indicating that the file is finished reading
res.on('end', () = > {// Result indicates the contents of the file
});
})
// 2. Read files synchronously
const data = fs.readFileSync('/path/to/file/');
Copy the code
As you can see, when retrieving file contents, the default is the buffer format, which is the API introduced in Node for handling binary data in TCP streams and file system operations.
2.2 readdir and readdirSync
These two functions are used in a similar way to readFile and readFileSync. They read a file path and return an array of file names in the corresponding file directory. Readdir (‘/filePath’,(err, list)=>{}); And const list = fs.readdirsync (‘/filePath’);
Module. Exports and require
We saw above how to use other modules in Node by using require. In Node, you can also develop your own modules and use them.
Sample code:
// module.js
module.exports = function(args) {
// Develop your own module logic here
}
// your program
const myModule = require('module.js'); .Copy the code
There are a few principles to follow when developing your own modules:
1. Export a function that takes exactly the parameters it needs
2. Module programs do not change anything else, such as global variables
3. Do not do any output in the module, the result of processing should be returned to the program used for processing
4. If your module is an asynchronous operation, you need to accept a callback as an argument
5. Use the callback function when an error occurs or data is available
Function callback(err, data){}
4. The HTTP module
The HTTP module is one of the most important core functions of Node and can be used to process HTTP requests and generate a simple HTTP service. Let’s take a look at a few simple application scenarios using HTTP modules.
/ / introduction
const http = require('http');
1. Make a simple HTTP request
http.get('/url'.function(err, res){
The // res argument is a stream object in Node
// can be interpreted as an object that triggers some events
res.on('data'.function(data){
// Listen on the res. The data obtained is a buffer object
});
res.on('end'.function() {}); });// 2. Generate a local server with port 3000
const server = http.createServer(function(req, res) {
// req indicates a request and res indicates a response
});
server.listen(3000);
Copy the code
Refer to the article
Learnyounode website
Node 中文 网
About the author: Gong Chenguang, Renhe front-end engineer of future big data.