This article will introduce to you how to use node to add, delete, modify and check local json files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Related recommendations: "nodejs tutorial"
Hello! The file system of nodejs. Anyone who has been in contact with node will definitely be familiar with the file system of node. In the past two days, I have been thinking about a question. Can I operate my local json file locally so that I can have a local text database? , if it is a recording software such as notes, I don’t need to connect to the backend database at all. I can operate the local json file myself and write the backend using node. The answer is yes. Let’s implement it together. Right Add, delete, modify and check local json files
##1. Add
First let’s take a look at the directory structure of the demo
Let’s take a look at the content of person.json
Nothing wrong, the next step is to witness the miracle
Write below The code of nodejs
var fs = require('fs');var params = { "id":5, "name":"白眉鹰王"}//在真实的开发中id肯定是随机生成的而且不会重复的,下一篇写如何生成随机切不会重复的随机数,现在就模拟一下假数据//写入json文件选项function writeJson(params){ //现将json文件读出来 fs.readFile('./mock/person.json',function(err,data){ if(err){ return console.error(err); } var person = data.toString();//将二进制的数据转换为字符串 person = JSON.parse(person);//将字符串转换为json对象 person.data.push(params);//将传来的对象push进数组对象中 person.total = person.data.length;//定义一下总条数,为以后的分页打基础 console.log(person.data); var str = JSON.stringify(person);//因为nodejs的写入文件只认识字符串或者二进制数,所以把json对象转换成字符串重新写入json文件中 fs.writeFile('./mock/person.json',str,function(err){ if(err){ console.error(err); } console.log('----------新增成功-------------'); }) })}writeJson(params)//执行一下;
The results are as follows
Let’s take a look at the results of the json file
Look, the json file has been added
##2. Delete
var fs = require('fs');//删除json文件中的选项function deleteJson(id){ fs.readFile('./mock/person.json',function(err,data){ if(err){ return console.error(err); } var person = data.toString(); person = JSON.parse(person); //把数据读出来删除 for(var i = 0; i <p><img src="https://img.php.cn/upload/article/000/000/024/958beac2084a4988806a16de07120258-4.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p><p><img src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-5.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p> <p>The fifth one was deleted</p><p><strong>##3. Change</strong></p><p>The json file at the beginning looks like this<br><img src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-6.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p> <p>The code below</p><pre class="brush:php;toolbar:false">var fs = require('fs');var params = { "name":"孙悟空"}function changeJson(id,params){ fs.readFile('./mock/person.json',function(err,data){ if(err){ console.error(err); } var person = data.toString(); person = JSON.parse(person); //把数据读出来,然后进行修改 for(var i = 0; i <p>The result is as follows</p><p><img src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-7.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p><p>Look at the json file</p><p><img src="https://img.php.cn/upload/article/000/000/024/e8c5e5bba1bc620b84cd435324187741-8.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p><p>Modify success! ! ! ! </p><p><strong>##4. Check</strong></p><p>Now simulate paging</p><p>First look at what the json file looks like</p><p><img src="https://img.php.cn/upload/article/000/000/024/e8c5e5bba1bc620b84cd435324187741-9.png" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"> </p><p>The above code</p><pre class="brush:php;toolbar:false">var fs = require('fs');//通过传回来的页数,进行分页模拟function pagination(p,s){ //p为页数,比如第一页传0,第二页传1,s为每页多少条数据 fs.readFile('./mock/person.json',function(err,data){ if(err){ console.error(err); } var person = data.toString(); person = JSON.parse(person); //把数据读出来 //console.log(person.data); var length = person.data.length; var pagePerson = person.data.slice(s*p,(p+1)*s); console.log('------------------------查询成功pagePerson'); console.log(pagePerson); })}pagination(0,6);//查询第一页,每页的数据条数为6条
The execution result is
Let’s take a look at the second page
Execute pagination(1, 6);
The execution result is
and so on, the effect of simulated paging is achieved
##5. Summary
ok, this is how I finished writing a basic set of additions, deletions, modifications, and searches. I feel that node is still very powerful, and I feel that I have made some progress. Although some people may think that it is meaningless to write like this, maybe, but I It feels very meaningful and I have gained a lot! ! !
For more knowledge about computer programming, please visit: Introduction to Programming! !
The above is the detailed content of Let's talk about how to use nodejs to add, delete, modify and check local json files. For more information, please follow other related articles on the PHP Chinese website!