path module
_u dirname: Get the absolute path directory of the current file
_u filename: Get the path to the current file
ctx.body = { dirName: __dirname, fileName: __filename } //{"dirName":"D:\\WEB\\koa_proj","fileName":"D:\\WEB\\koa_proj\\app.js"}
path.join(p1, p2): for splicing paths
//"D:\\WEB\\koa_proj\\file.txt" path.join(__dirname, '/file.txt');
path.basename(path, ext): Used to get the file name in the path including the suffix name
/** ext A file suffix name, such as.txt, with this parameter takes the file name, excluding the suffix **/ let filePath = path.join(__dirname, '/file.txt') path.basename(filePath)//"file.txt" path.basename(filePath, '.txt')//"file"
path.dirname(path): The name of the directory used to get a full file path, similar to u dirname
let filePath = path.join(__dirname, '/file.txt'); //"D:\\WEB\\koa_proj" path.dirname(filePath)
path.delimiter: delimiter, in windows';', in linux':'
path.extname(p): the name or name used to get the file
path.extname(filePath)//".txt"
path.parse(p): The path used to parse a file and generate an object that includes the directory, file name, and file suffix
path.parse(filePath) /* { "root":"D:\\", "dir":"D:\\WEB\\koa_proj", "base":"file.txt", "ext":".txt", "name":"file" } */
path.format: Used to convert an object to its path, as opposed to the path.parse process
path.isAbsolute(p): Used to determine whether a file change is a relative or absolute path
path.normalize(p): Output file paths\, depending on the operating system, will be processed uniformly with inconsistent delimiters in the paths
path.normalize('D:/WEB\\koa_proj') //"normalizePath":"D:\\WEB\\koa_proj"
path.relative(from, to): Used to get the relative path from the from path to the to path
path.resolve(p1, p2): For path splicing, the absolute path relative to the application is always obtained, path.resolve:** follows the path with/so the last/file is used as the base for string splicing
path.resolve(__dirname, './public','./file.txt') //"D:\\WEB\\koa_proj\\public\\file.txt"
path.sep: Gets the file path action symbol for the current system, which is'//'in window
app.use(route.get('/index', async ctx => { console.log(__dirname); console.log(__filename); ctx.body = { dirName: __dirname, fileName: __filename , filePath: path.join(__dirname, './file.txt'), extFileName: path.basename(filePath, '.txt'), dirPathName: path.dirname(filePath), pathObj: path.parse(filePath), normalizePath: path.normalize('D:/WEB\\koa_proj'), relativePath: path.relative(path.join(__dirname, 'file.txt'), path.join(__dirname, '/js/file.js')), jsPath: path.join(__dirname, relativePath), resolvePath: path.resolve(__dirname, './public','./file.txt'), resolvePath2: path.resolve(__dirname, '/js/index.js', '/css/index.css'), sep: path.sep } }));
fs module
fs.statSync(p, callback): Used to detect the existence of a file and error occurs when the file does not exist: no such file or directory, stat'D:WEBkoa_projfile.txt'
fs.statSync(__filename); {"dev":2585188530,"mode":33206,"nlink":1,"uid":0,"gid":0,"rdev":0,"ino":844424930559515,"size":777,"atimeMs":1526185196399,"mtimeMs":1526185196399,"ctimeMs":1526185196399,"birthtimeMs":1524389744769,"atime":"2018-05-13T04:19:56.399Z","mtime":"2018-05-13T04:19:56.399Z","ctime":"2018-05-13T04:19:56.399Z","birthtime":"2018-04-22T09:35:44.769Z"}
fs.writeFileSync(p, data): Used to write data to a file and create a corresponding file if it does not exist
fs.writeFileSync(path.join(__dirname, './index.js'), 'hello word')
fs.appendFileSync(p, data[,option]): Used to append content to a file and create it if it does not exist
fs.appendFileSync(path.join(__dirname, './index.js'), 'hi');
fs.unlink(p): Used to delete a file and error if the file does not exist or the deletion fails
fs.unlinkSync(path.join(__dirname, './index.js'));
fs.readFileSync(p[,option]): Used to read file contents
/* *Data read when no encoding format is specified is Buffer */ fs.readFileSync(path.join(__dirname, './index.js'), 'utf-8');
fs.renameSync(oldPath, newPath): Used to move or rename files, the directory must exist when moving the directory
fs.renameSync(path.join(__dirname, './index.js'), path.join(__dirname, './index2.js'));
fs.mkdir(p, name): Used to create a directory, the directory created must not exist, otherwise an error will be reported
fs.mkdirSync(path.join(__dirname, 'test'));
fs.rmdirSync(p): Used to delete a file directory
fs.rmdirSync(path.join(__dirname, './test'));
fs.readdirSysn(p): Used to read files and directories under a directory
fs.readdirSync(path.join(__dirname, './js'));