In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the role of fs in nodejs". In daily operation, I believe that many people have doubts about the role of fs in nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the role of fs in nodejs?" Next, please follow the editor to study!
Fs in nodejs is the abbreviation of "file system" file system, and the file operation API,fs module provided by NodeJS is used to read and write system files and directories. All methods of fs module have synchronous and asynchronous modes.
This tutorial operating environment: windows10 system, nodejs version 12.19.0, DELL G3 computer.
What does fs mean in nodejs
Introduction of 1.fs module
The full name of fs is file system (File system), which is the file manipulation API provided by NodeJS. Fs module is used to read and write system files and directories. It is a very important module, and the operation of files is based on it. All the methods of this module have synchronous and asynchronous methods, so let's briefly introduce the common methods of fs module.
two。 Use the fs module for simple read and write operations
Read file = > readFile (asynchronous read) and readFileSync (synchronous read)
Fs.readFile (file_name [, options], function (err,data) {/ / Asynchronous read with two required parameters and an optional parameter / / required parameter: file_name file path name, callback callback function, the first parameter of the callback function is to read the error message, and the second is the data in the file / / an optional parameter: options this parameter is an object and contains {encoding, flag}. Default encoding is binary, flag is'w'}) fs.readFileSync (file_name [, options]) / / synchronous read with a required parameter and an optional parameter, as above
The simple implementation is as follows:
Var fs=require ('fs'); / introduce fs module / / Asynchronous read fs.readFile (' aaa.txt',function (err,data) {if (err) {console.log ('read error');} else {console.log ('Asynchronous read:' + data.toString () / / because data returns binary data, you need to use the toString () method to convert or fill in optional parameters with text encoding utf-8}}); fs.readFile ('aaa.txt','utf-8',function (err,data) {if (err) {console.log (' read error');} else {console.log ('Asynchronous read:' + data);}}) / / synchronous read var data=fs.readFileSync ('aaa.txt'); console.log (' synchronous read:'+ data.toString ()); var data=fs.readFileSync ('aaa.txt','utf-8'); console.log (' synchronous read:'+ data)
The results are shown as follows:
two。 Write file = > writeFile (asynchronous write) and writeFileSync (synchronous write)
WriteFile () opens the file directly in w mode by default, so if the file exists, the content written by this method will overwrite the old file content, and a new file will be created if the file does not exist.
Fs.writeFile (file_name,data [, options], function (err) {/ / Asynchronous write takes three required parameters and one optional parameter / / three required parameters: file_name file name, file information written by data, function a callback function, and the callback function takes a write error message / / an optional parameter: options this parameter is an object containing {encoding, mode, flag}. Default encoding is utf8, mode is 0666, flag is'w'}) fs.writeFile (file_name,data [, options]) / / synchronous write with two required parameters and one optional parameter, the parameter meaning is one less callback function var fs=require ('fs'); / / write fs.writeFile asynchronously (' aaa.txt',' writes file information', function (err) {console.log (err)) }) / / write fs.writeFileSync synchronously ('aaa.txt',' write file information')
The results show:
4. A little practice on the module
Here, combine the fs module with the http module contacted earlier to make a small integration, and write a simple small demo that is created from the server to find the file in response to the foreground and return accordingly:
Var http=require ('http'); var fs=require (' fs'); var querystring=require ('querystring'); var urlLib=require (' url'); http.createServer (function (req,res) {/ / GET request parsing data var obj=urlLib.parse (req.url,true); var url=obj.pathname; var GET=obj.query; / / POST request parsing data var str=''; res.on ('data',function (data) {str+=data) }) res.on ('end',function () {var POST=querystring (str);}) / / File request var file_name='./www'+url; fs.readFile (file_name,function (err,data) {if (err) {res.write (' 404'); / / File not found returns 404} else {res.write (data) / / find the file return file information} res.end ();})}. Please (8080) at this point, the study on "what is the role of fs in nodejs" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.