nodejs渐入佳境[10]-案例-用户输入+json增删查改-创新互联
对于之前的代码进行封装
成都创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为四川企业提供专业的成都网站制作、做网站,
四川网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
原始文件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
| //打印字符串 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:
封装 获取节点以及保存节点、返回节点。
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');
//从文件中获取节点 var fetchNode = ()=>{ try{ var notesString = fs.readFileSync('notes-data.json'); return JSON.parse(notesString); }catch(e){ return []; } } //保存节点到文件 var saveNote = (notes)=>{
fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}
//增加节点,如果新增返回新增节点。 var addNote = (title,body)=>{ var notes = fetchNode(); var note = { title, body };
//筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); //没有相同的节点 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 };
|
测试
打开控制台,在当前目录下输入:
1
| > node app.js add --title="buy book3" --body="jonson"
|
控制台返回结果:
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
|
移除节点
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'); //从文件中获取节点 var fetchNode = ()=>{ try{ var notesString = fs.readFileSync('notes-data.json'); return JSON.parse(notesString); }catch(e){ return []; } } //保存节点到文件 var saveNote = (notes)=>{
fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}
//增加节点,如果新增返回新增节点。 var addNote = (title,body)=>{ var notes = fetchNode(); var note = { title, body };
//筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); //没有相同的节点 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(); //筛选出不同的节点 var duplicateNotes = notes.filter((note)=>note.title!==title); saveNote(duplicateNotes); return notes.length !==duplicateNotes.length; };
module.exports = { addNote, getAll, getNote, removeNote };
|
测试2
打开控制台,在当前目录下输入:
1
| > node app.js remove --title="buy book2"
|
控制台返回结果并且josn文件中对应元素被移除:
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
|
获取节点
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'); //从文件中获取节点 var fetchNode = ()=>{ try{ var notesString = fs.readFileSync('notes-data.json'); return JSON.parse(notesString); }catch(e){ return []; } } //保存节点到文件 var saveNote = (notes)=>{
fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}
//增加节点,如果新增返回新增节点。 var addNote = (title,body)=>{ var notes = fetchNode(); var note = { title, body };
//筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); //没有相同的节点 if(duplicateNotes.length ===0){ notes.push(note); saveNote(notes); return note; } }
var getAll = ()=>{
};
var getNote = (title)=>{
var notes = fetchNode(); //筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); return duplicateNotes[0]; };
var removeNote = (title)=>{ var notes = fetchNode(); //筛选出不同的节点 var duplicateNotes = notes.filter((note)=>note.title!==title); saveNote(duplicateNotes); return notes.length !==duplicateNotes.length; };
module.exports = { addNote, getAll, getNote, removeNote };
|
测试3
打开控制台,在当前目录下输入:
1
| > node app.js read --title="buy book3"
|
控制台返回结果:
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
|
列出所有节点
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'); //从文件中获取节点 var fetchNode = ()=>{ try{ var notesString = fs.readFileSync('notes-data.json'); return JSON.parse(notesString); }catch(e){ return []; } } //保存节点到文件 var saveNote = (notes)=>{
fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}
//增加节点,如果新增返回新增节点。 var addNote = (title,body)=>{ var notes = fetchNode(); var note = { title, body };
//筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); //没有相同的节点 if(duplicateNotes.length ===0){ notes.push(note); saveNote(notes); return note; } }
var getAll = ()=>{ var notes = fetchNode(); return notes; };
var getNote = (title)=>{
var notes = fetchNode(); //筛选出相同的节点 var duplicateNotes = notes.filter((note)=>note.title===title); return duplicateNotes[0]; };
var removeNote = (title)=>{ var notes = fetchNode(); //筛选出不同的节点 var duplicateNotes = notes.filter((note)=>note.title!==title); saveNote(duplicateNotes); return notes.length !==duplicateNotes.length; };
|
测试4
打开控制台,在当前目录下输入:
控制台返回结果:
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' }
|
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章标题:nodejs渐入佳境[10]-案例-用户输入+json增删查改-创新互联
链接分享:
http://pwwzsj.com/article/dcsoss.html