溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nodejs漸入佳境[10]-案例-用戶輸入+json增刪查改

發(fā)布時(shí)間:2020-07-23 18:51:44 來(lái)源:網(wǎng)絡(luò) 閱讀:339 作者:jonson_jackson 欄目:開發(fā)技術(shù)

對(duì)于之前的代碼進(jìn)行封裝

原始文件app.js

對(duì)于返回節(jié)點(diǎn)進(jìn)行判斷,如果新增了節(jié)點(diǎn),就打印新增節(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
//打印字符串
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'){
  var note = nodes.addNote(argv.title,argv.body);
 if(note){
   console.log('add success');
   console.log(`title:${note.title}`);
   console.log(`body:${note.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');
}

notes.js:

封裝 獲取節(jié)點(diǎn)以及保存節(jié)點(diǎn)、返回節(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
47
48
49
50
51
52
53
54
55
56
console.log('start nodes.js');
const fs = require('fs');

//從文件中獲取節(jié)點(diǎn)
var fetchNode = ()=>{
 try{
   var notesString = fs.readFileSync('notes-data.json');
   return JSON.parse(notesString);
 }catch(e){
   return [];
 }
}
//保存節(jié)點(diǎn)到文件
var saveNote = (notes)=>{

 fs.writeFileSync('notes-data.json',JSON.stringify(notes));

}

//增加節(jié)點(diǎn),如果新增返回新增節(jié)點(diǎn)。
var addNote = (title,body)=>{
 var notes = fetchNode();
 var note = {
     title,
     body
 };

 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //沒(méi)有相同的節(jié)點(diǎn)
 if(duplicateNotes.length ===0){
   notes.push(note);
   saveNote(notes);
   return note;
 }
}

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
};

測(cè)試

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

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

控制臺(tái)返回結(jié)果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'add',
 '--title=buy book3',
 '--body=jonson' ]
yargs { _: [ 'add' ],
 title: 'buy book3',
 body: 'jonson',
 '$0': 'app.js' }
add success
title:buy book3
body:jonson

移除節(jié)點(diǎn)

app.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
//打印字符串
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'){
  var note = nodes.addNote(argv.title,argv.body);
 if(note){
   console.log('add success');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }
}else if(command === 'list'){
 nodes.getAll();

}else if(command =='read'){
 nodes.getNote(argv.title);
}else if(command=='remove'){
 var noteRemoved =  nodes.removeNote(argv.title);
 var message = noteRemoved?'Note was removed':'note not found';
 console.log(message);
}else{
 console.log('command not find');
}

notes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
console.log('start nodes.js');
const fs = require('fs');
//從文件中獲取節(jié)點(diǎn)
var fetchNode = ()=>{
 try{
   var notesString = fs.readFileSync('notes-data.json');
   return JSON.parse(notesString);
 }catch(e){
   return [];
 }
}
//保存節(jié)點(diǎn)到文件
var saveNote = (notes)=>{

 fs.writeFileSync('notes-data.json',JSON.stringify(notes));

}

//增加節(jié)點(diǎn),如果新增返回新增節(jié)點(diǎn)。
var addNote = (title,body)=>{
 var notes = fetchNode();
 var note = {
     title,
     body
 };

 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //沒(méi)有相同的節(jié)點(diǎn)
 if(duplicateNotes.length ===0){
   notes.push(note);
   saveNote(notes);
   return note;
 }
}

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

var getNote = (title)=>{

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

var removeNote = (title)=>{
 var notes = fetchNode();
 //篩選出不同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title!==title);
   saveNote(duplicateNotes);
   return notes.length !==duplicateNotes.length;
};

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

測(cè)試2

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

1
> node app.js remove --title="buy book2"

控制臺(tái)返回結(jié)果并且josn文件中對(duì)應(yīng)元素被移除:

1
2
3
4
5
6
7
8
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'remove',
 '--title=buy book2' ]
yargs { _: [ 'remove' ], title: 'buy book2', '$0': 'app.js' }
Note was removed

獲取節(jié)點(diǎn)

app.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
//打印字符串
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'){
  var note = nodes.addNote(argv.title,argv.body);
 if(note){
   console.log('add success');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }
}else if(command === 'list'){
 nodes.getAll();

}else if(command =='read'){
 var note = nodes.getNote(argv.title);
 if(note){
   console.log('find');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }else{
   console.log('note not found');
 }
}else if(command=='remove'){
 var noteRemoved =  nodes.removeNote(argv.title);
 var message = noteRemoved?'Note was removed':'note not found';
 console.log(message);
}else{
 console.log('command not find');
}

notes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
console.log('start nodes.js');
const fs = require('fs');
//從文件中獲取節(jié)點(diǎn)
var fetchNode = ()=>{
 try{
   var notesString = fs.readFileSync('notes-data.json');
   return JSON.parse(notesString);
 }catch(e){
   return [];
 }
}
//保存節(jié)點(diǎn)到文件
var saveNote = (notes)=>{

 fs.writeFileSync('notes-data.json',JSON.stringify(notes));

}

//增加節(jié)點(diǎn),如果新增返回新增節(jié)點(diǎn)。
var addNote = (title,body)=>{
 var notes = fetchNode();
 var note = {
     title,
     body
 };

 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //沒(méi)有相同的節(jié)點(diǎn)
 if(duplicateNotes.length ===0){
   notes.push(note);
   saveNote(notes);
   return note;
 }
}

var getAll = ()=>{

};

var getNote = (title)=>{

 var notes = fetchNode();
 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 return duplicateNotes[0];
};

var removeNote = (title)=>{
 var notes = fetchNode();
 //篩選出不同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title!==title);
   saveNote(duplicateNotes);
   return notes.length !==duplicateNotes.length;
};

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

測(cè)試3

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

1
> node app.js read  --title="buy book3"

控制臺(tái)返回結(jié)果:

1
2
3
4
5
6
7
8
9
10
11
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'read',
 '--title=buy book3',
 '--body=123' ]
yargs { _: [ 'read' ], title: 'buy book3', body: 123, '$0': 'app.js' }
find
title:buy book3
body:jonson

列出所有節(jié)點(diǎn)

app.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
//打印字符串
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'){
  var note = nodes.addNote(argv.title,argv.body);
 if(note){
   console.log('add success');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }
}else if(command === 'list'){
   var allnotes =  nodes.getAll();
   allnotes.forEach((note)=>{  console.log(note)});
}else if(command =='read'){
 var note = nodes.getNote(argv.title);
 if(note){
   console.log('find');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }else{
   console.log('note not found');
 }
}else if(command=='remove'){
 var noteRemoved =  nodes.removeNote(argv.title);
 var message = noteRemoved?'Note was removed':'note not found';
 console.log(message);
}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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
console.log('start nodes.js');
const fs = require('fs');
//從文件中獲取節(jié)點(diǎn)
var fetchNode = ()=>{
 try{
   var notesString = fs.readFileSync('notes-data.json');
   return JSON.parse(notesString);
 }catch(e){
   return [];
 }
}
//保存節(jié)點(diǎn)到文件
var saveNote = (notes)=>{

 fs.writeFileSync('notes-data.json',JSON.stringify(notes));

}

//增加節(jié)點(diǎn),如果新增返回新增節(jié)點(diǎn)。
var addNote = (title,body)=>{
 var notes = fetchNode();
 var note = {
     title,
     body
 };

 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //沒(méi)有相同的節(jié)點(diǎn)
 if(duplicateNotes.length ===0){
   notes.push(note);
   saveNote(notes);
   return note;
 }
}

var getAll = ()=>{
 var notes = fetchNode();
 return notes;
};

var getNote = (title)=>{

 var notes = fetchNode();
 //篩選出相同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title===title);
 return duplicateNotes[0];
};

var removeNote = (title)=>{
 var notes = fetchNode();
 //篩選出不同的節(jié)點(diǎn)
 var duplicateNotes = notes.filter((note)=>note.title!==title);
   saveNote(duplicateNotes);
   return notes.length !==duplicateNotes.length;
};

測(cè)試4

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

1
> node app.js list

控制臺(tái)返回結(jié)果:

1
2
3
4
5
6
7
8
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'list' ]
yargs { _: [ 'list' ], '$0': 'app.js' }
{ title: 'buy book3', body: 'jonson' }
{ title: 'buy book2', body: 'jonson' }
  • 本文鏈接: https://dreamerjonson.com/2018/11/14/node-10/

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

nodejs漸入佳境[10]-案例-用戶輸入+json增刪查改

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI