path模块
const path = require ( 'path' ) ;
const filePath = '/home/user/docs/file.txt' ;
console. log ( path. basename ( filePath) ) ;
console. log ( path. extname ( filePath) ) ;
console. log ( path. dirname ( filePath) ) ;
const joinPath = path. join ( '/node' , './index.js' )
console. log ( joinPath) ;
const resolvePath = path. resolve ( 'node' , './index.js' )
console. log ( resolvePath) ;
const currentPath = path. resolve ( ) ;
console. log ( currentPath) ;
console. log ( '当前文件所在目录的绝对路径是:' , __dirname) ;
const joinPath1 = path. join ( __dirname, '../package.json' ) ;
console. log ( joinPath1) ;
fs模块
const fs = require ( 'fs' ) ;
const fsp = require ( 'fs/promises' ) ;
fs. readFile ( './index1.txt' , 'utf8' , ( err, data ) => {
if ( ! err) {
console. log ( data. toString ( ) ) ;
}
} )
let txt = fs. readFileSync ( './index1.txt' , 'utf8' ) ;
console. log ( txt. toString ( ) , '同步' ) ;
const readStream = fs. createReadStream ( './index1.txt' , 'utf8' ) ;
readStream. on ( 'data' , ( chunk ) => {
console. log ( chunk, 'chunk' ) ;
} )
readStream. on ( 'end' , ( ) => {
console. log ( 'close' ) ;
} )
const writeStreamList = [ '创建读取流,适用于读取大文件' , '创建写入流,适用于携入大文件' , '开启 recursive,递归创建' ] ;
const writeStream = fs. createWriteStream ( './index1.txt' ) ;
writeStreamList. forEach ( item => {
writeStream. write ( item + '\n' )
} )
writeStream. end ( )
writeStream. on ( 'finish' , ( ) => {
console. log ( '写入完成' )
} )
fs. mkdir ( 'path/test/ccc' , { recursive: true } , ( err ) => {
console. log ( err) ;
} )
fs. rm ( './path/test/ccc' , ( err ) => {
} )
fs. watch ( './index1.txt' , ( event, filename ) => {
console. log ( event, filename)
} )
fs. writeFileSync ( 'index1.txt' , '\nvue之父\n鱿鱼须' , {
flag: 'a'
} )
fs. appendFile ( 'index1.txt' , '\nunshift创始人\n麒麟哥' , ( ) => {
console. log ( '追加完成' ) ;
} )
fs. linkSync ( './index1.txt' , './index2.txt' )
fs. symlinkSync ( './index1.txt' , './index3.txt' , "file" )
crypto模块
const crypto = require ( 'crypto' ) ;
let text = '123456' ;
const hash = crypto. createHash ( 'md5' ) ;
hash. update ( text) ;
const hashValue = hash. digest ( 'hex' ) ;
console. log ( 'Hash:' , hashValue) ;