


Let's talk about how to use nodejs to add, delete, modify and check local json files
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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/958beac2084a4988806a16de07120258-4.png" class="lazy" alt="Lets talk about how to use nodejs to add, delete, modify and check local json files"></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-5.png" class="lazy" 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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-6.png" class="lazy" 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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/57338fe94b8097b8ba5e3adc61d9c0c0-7.png" class="lazy" 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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/e8c5e5bba1bc620b84cd435324187741-8.png" class="lazy" 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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/e8c5e5bba1bc620b84cd435324187741-9.png" class="lazy" 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application
