nodejs漸入佳境[9]-保存節(jié)點(diǎn)到j(luò)son文件-創(chuàng)新互聯(lián)

原始文件

app.js:

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、慶陽(yáng)網(wǎng)絡(luò)推廣、小程序開(kāi)發(fā)、慶陽(yáng)網(wǎng)絡(luò)營(yíng)銷、慶陽(yáng)企業(yè)策劃、慶陽(yáng)品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供慶陽(yáng)建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const yargs = require('yargs');
const nodes = require('./nodes.js')
console.log('Start app.');

console.log(process.argv);

console.log('yargs',yargs.argv);
const argv = yargs.argv;
var command = process.argv[2];

if(command==='add'){
 nodes.addNote(argv.title,argv.body);
}else if(command === 'list'){
 nodes.getAll();

}else if(command =='read'){
 nodes.getNote(argv.title);
}else if(command=='remove'){
 nodes.removeNote(argv.title);
}else{
 console.log('command not find');
}

nodes.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
console.log('start nodes.js');
const fs = require('fs');
var addNote = (title,body)=>{
 var notes = [];
 var note = {
     title,
     body
 };

 try{
   //讀取json文件,讀出來(lái)是string
   var notesString = fs.readFileSync('notes-data.json');
   // string轉(zhuǎn)換為json對(duì)象
   notes = JSON.parse(notesString);
 }catch(e){

 }
 //增加
 notes.push(note);
 //保存
 fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}

var getAll = ()=>{
console.log('Get All notes');
};

var getNote = (title)=>{

 console.log('getting note',title);
};

var removeNote = (title)=>{
 console.log('Removing note',title);
};

module.exports = {
   addNote,
   getAll,
   getNote,
   removeNote
};

打開(kāi)控制臺(tái),在當(dāng)前目錄下輸入:

1
> node app.js add --title="buy book2" --body="jonson"

將節(jié)點(diǎn)添加到notes-data.json文件中.

再次輸入:

1
> node app.js add --title="buy book2" --body="jonson"

notes-data.json:

1
[{"title":"buy book2","body":"jonson"},{"title":"buy book2","body":"jonson"}]

改進(jìn) 不添加重復(fù)的節(jié)點(diǎn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
console.log('start nodes.js');
const fs = require('fs');
var addNote = (title,body)=>{
 var notes = [];
 var note = {
     title,
     body
 };

 try{
   var notesString = fs.readFileSync('notes-data.json');
   notes = JSON.parse(notesString);
 }catch(e){

 }

 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //沒(méi)有相同的節(jié)點(diǎn)
 if(duplicateNotes.length ===0){
   notes.push(note);
   fs.writeFileSync('notes-data.json',JSON.stringify(notes));
 }


}

var getAll = ()=>{
console.log('Get All notes');
};

var getNote = (title)=>{

 console.log('getting note',title);
};

var removeNote = (title)=>{
 console.log('Removing note',title);
};

module.exports = {
   addNote,
   getAll,
   getNote,
   removeNote
};

再次輸入不會(huì)添加節(jié)點(diǎn):

1
> node app.js add --title="buy book2" --body="jonson"
  • 本文鏈接: https://dreamerjonson.com/2018/11/13/node-9/

  • 版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉(zhuǎn)載請(qǐng)注明出處!

nodejs漸入佳境[9]-保存節(jié)點(diǎn)到j(luò)son文件

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買多久送多久。

網(wǎng)頁(yè)名稱:nodejs漸入佳境[9]-保存節(jié)點(diǎn)到j(luò)son文件-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article46/dejghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈定制網(wǎng)站、網(wǎng)站導(dǎo)航Google、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司