Home >Web Front-end >JS Tutorial >How to create files in nodejs
Nodejs method of creating files: first create a front-end sample file; then create files through the fs core module in NodeJS. The module method for creating files is such as "fs.mkdir".
The operating environment of this article: Windows 7 system, nodejs version 14.16, Dell G3 computer.
nodejs creates files and writes file contents
In NodeJS, all file operations are implemented through the fs core module, including the creation and deletion of file directories. , query, and file reading and writing. In the fs module, all methods are divided into two types: synchronous and asynchronous. Methods with the sync suffix are synchronous methods, and methods without the sync suffix are asynchronous methods.
fs module method introduction:
fs.stat 检测是文件还是目录(目录 文件是否存在) fs.mkdir 创建目录 (创建之前先判断是否存在) fs.writeFile 写入文件(文件不存在就创建,但不能创建目录) fs.appendFile 写入追加文件 fs.readFile 读取文件 fs.readdir 读取目录 fs.rename 重命名 fs.rmdir 删除目录 fs.unlink 删除文件
nodejs creates a file and writes the content:
//写入文件(会覆盖之前的内容)(文件不存在就创建) utf8参数可以省略 fs.writeFile('123.txt','你好nodejs 覆盖','utf8',function(error){ if(error){ console.log(error); return false; } console.log('写入成功'); })
Recommended: "nodejs video tutorial"
The above is the detailed content of How to create files in nodejs. For more information, please follow other related articles on the PHP Chinese website!