1,
1)
The method or idea of using packages needs to come first
- The first step is to find the package you want to use at npmjs.com
- Step 2 Install the package
- Step 3 Look at the documentation how to use this package
2) For example, if you want to use MD5 encryption module, how to operate?
- NPM init –yes generates package.json
- Then go to npmjs.com and search for MD5 and see how to use it
- NPM install MD5
- Method of use
3)
- Found the test successful!
Here’s a tip for installing modules: Save puts the corresponding module in package.json dependencies
2, Another example of formatting time silly-time
NPM I silly-datetime –save
use
var sd = require('silly-datetime');
var time = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
console.log(time)
Copy the code
The results of
2) Of course a fileIt is possible to introduce multiple packages
- this
Very important!!
- This one needs to be noticed as well
3)
3. Fs module
Demonstrate each of the commonly used methods
1)Fs.stat checks whether it is a file or a directory
- Write this in app.js
Const fs = require("fs") fs.stat('./ HTML ',(err,data)=>{if(err){console.log(err); return; } console.log(' is a file: ${data.isfile ()} '); Console. log(' isDirectory: ${data.isdirectory ()} ')})Copy the code
The results of
2) fs.mkdir create a directory
// 2, fs.mkdir create directory const fs = require("fs") fs.mkdir('./ CSS ',(err)=>{if(err){console.log(err); return ; } console.log(' created successfully ')})Copy the code
- There is one caveat
If you run it again it will give you an error because it has already been created
3) fs.writefile () creates write files
// 3, fs.writefile () const fs = require("fs") fs.writefile ('./ HTML /index.html'," hello nodejs",(err)=>{if(err){ console.log(err); return; } console.log(' write succeeded ')})Copy the code
-
It turns out to be as expected isn’t that nice
-
One important thing to note is that the changes overwrite the previous content when executed again
4) fs.appendFile() appends the file
Const fs = require("fs") fs.appendFile("./ CSS /base.css",'body{color:red}',(err)=>{if(err){// 4, appendFile() const fs = require("fs") fs. console.log(err); return; } console.log('appendFile succeeded ')})Copy the code
- It worked
- One thing to note
AppendFile appends files that are created first if they are not
5) Fs.readFile () reading file demo
- Take reading index.js as an example
// 3, fs.readfile () const fs = require("fs") fs.readfile ("./ HTML /index.html",(err,data)=>{if(err){// 4, fs.readfile () const fs = require("fs") fs.readfile ("./ HTML /index.html",(err,data)=>{if(err){ console.log(err); return; } console.log(data) console.log(data.tostring ()) // convert Buffer toString type})Copy the code
6) fs.readdir() reads the directory to get folders and files in the directory
Const fs = require("fs") fs.readdir("./ HTML ",(err,data)=>{if(err){console.log(err); // 3, fs.readdir() const fs = require("fs") fs.readdir("./ HTML ",(err,data)=>{if(err){console.log(err); return; } console.log(data) })Copy the code
7) fs.rename Rename/move a file
// 7, fs.rename rename file const fs = require("fs") fs.rename("./ CSS /index.css","./ CSS /base.css",(err)=>{if(err){// 7, fs.rename rename file const fs = require("fs") fs.rename("./ CSS /index.css","./ CSS /base.css",(err)=>{if(err){ console.log(err); return; } console.log(' success ')})Copy the code
Moving the files / / fs. Rename (". / CSS/base. CSS ", ". / HTML/base. CSS ", (err) = > {the if (err) {the console. The log (err); return; } console.log(' move succeeded ')})Copy the code
8) fs.unlink deletes files
// 8, fs.unlink delete file const fs = require("fs") fs.unlink('./aaa/index.js',(err)=>{if(err){console.log(err); // 8, fs.unlink delete file const fs = require("fs") fs.unlink('. return; } console.log(' deleted file successfully ')})Copy the code
9) fs.rmdir delete directory (you need to use fs.unlink to delete the files under the directory)
// 9, fs.rmdir delete directory const fs = require("fs") fs.rmdir('./aaa',(err)=>{if(err){console.log(err); return; Console. log(' directory deleted successfully ')})Copy the code