[微精通:網頁設計Vue + Node.js + MySQL] 4.2.CRUD

資料的操作不外乎四個動作 - CRUD,即新增Create、查詢Read、修改Update與刪除Delete。

 

image/svg+xml4.2.CRUD var express = require( 'express' ); var api = express(); var zombies = [ {id: 1 , name: ' 瘋狂傑克 ' , age: 3.3 , isVegetarian: true , typeImgSrc: "z1.png" }, {id: 2 , name: ' 懶懶瑪琍 ' , age: 10 , isVegetarian: true , typeImgSrc: "z2.png" }, {id: 3 , name: ' ⼤提姆 ' , age: 5 , isVegetarian: false , typeImgSrc: "z3.png" }, {id: 4 , name: ' 史提夫 ' , age: 2 , isVegetarian: false , typeImgSrc: "z4.png" }, {id: 5 , name: ' 功夫李 ' , age: 1 , isVegetarian: true , typeImgSrc: "z5.png" } ] ; api. post ( '/api/ getZombies ' , function (req, res) { var result = { data : null , error : null } ; result . data = zombies ; res.send ( JSON.stringify ( result )); } ); api.listen( 80 , function () { console.log( 'Zombie api listening on port 80!' ); }); var express = require( 'express' ); var api = express(); var zombies = [ {id: 1 , name: ' 瘋狂傑克 ' , age: 3.3 , isVegetarian: true , typeImgSrc: "z1.png" }, {id: 2 , name: ' 懶懶瑪琍 ' , age: 10 , isVegetarian: true , typeImgSrc: "z2.png" }, {id: 3 , name: ' ⼤提姆 ' , age: 5 , isVegetarian: false , typeImgSrc: "z3.png" }, {id: 4 , name: ' 史提夫 ' , age: 2 , isVegetarian: false , typeImgSrc: "z4.png" }, { id : 5 , name: ' 功夫李 ' , age: 1 , isVegetarian: true , typeImgSrc: "z5.png" } ] ; var maxID = 5 ; // 略過⼀些程式 api. post ( '/api/ addZombie ' , express.text() , function (req, res) { var result = { data: null , error: null } ; try { let zombie = JSON.parse ( req . body ); if ( typeof zombie.name != 'string' || zombie.name.trim() == "" ) { result . error = " 請輸入名字且必須為字串。 " ; res.send ( JSON.stringify ( result )); return ; } if ( typeof zombie.age != 'number' ){ result.error = " 請輸入變異年份且必須為數值。 " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.isVegetarian != 'boolean' ){ result.error = " 請輸入是否為素食者且必須為布林值。 " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.typeImgSrc != 'string' || [ 'z1.png' , 'z2.png' , 'z3.png' , 'z4.png' , 'z5.png' ].indexOf(zombie.typeImgSrc) == - 1 ){ result.error = " 請輸入類型圖片,且值必須為 z1.png z2.png z3.png z4.png z5.png " ; res.send(JSON.stringify(result)); return ; } maxID = maxID + 1 ; let newID = maxID ; let newZombie = { id: newID, name: zombie.name, age: zombie.age, isVegetarian: zombie.isVegetarian, typeImgSrc: zombie.typeImgSrc } ; zombies.push ( newZombie ); result.data = newZombie ; res.send( JSON.stringify ( result )); } catch (e) { result.error = e.toString(); res.send(JSON.stringify(result)); } } ); api.listen( 80 , function () { console.log( 'Zombie api listening on port 80!' ); }); var express = require( 'express' ); var api = express(); var zombies = [ {id: 1 , name: ' 瘋狂傑克 ' , age: 3.3 , isVegetarian: true , typeImgSrc: "z1.png" }, {id: 2 , name: ' 懶懶瑪琍 ' , age: 10 , isVegetarian: true , typeImgSrc: "z2.png" }, {id: 3 , name: ' ⼤提姆 ' , age: 5 , isVegetarian: false , typeImgSrc: "z3.png" }, {id: 4 , name: ' 史提夫 ' , age: 2 , isVegetarian: false , typeImgSrc: "z4.png" }, { id : 5 , name: ' 功夫李 ' , age: 1 , isVegetarian: true , typeImgSrc: "z5.png" } ] ; var maxID = 5 ; // 略過⼀些程式 api. post ( '/api/ updateZombie ' , express.text(), function (req, res) { var result = { data: null , error: null }; try { let zombie = JSON.parse ( req.body ); if ( typeof zombie.id != 'number' || zombies.map (x => x. id ). indexOf (zombie.id) == - 1 ){ result.error = " 請輸入有效的 id " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.name != 'string' || zombie.name.trim() == "" ){ result.error = " 請輸入名字且必須為字串。 " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.age != 'number' ){ result.error = " 請輸入變異年份且必須為數值。 " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.isVegetarian != 'boolean' ){ result.error = " 請輸入是否為素食者且必須為布林值。 " ; res.send(JSON.stringify(result)); return ; } if ( typeof zombie.typeImgSrc != 'string' || [ 'z1.png' , 'z2.png' , 'z3.png' , 'z4.png' , 'z5.png' ].indexOf(zombie.typeImgSrc) == - 1 ){ result.error = " 請輸入類型圖片,且值必須為 z1.png z2.png z3.png z4.png z5.png " ; res.send(JSON.stringify(result)); return ; } let index = zombies.map(x => x.id). indexOf ( zombie.id ); zombies [ index ]. name = zombie.name ; zombies [ index ]. age = zombie.age ; zombies [ index ]. isVegetarian = zombie.isVegetarian ; zombies [ index ]. typeImgSrc = zombie.typeImgSrc ; result.data = zombies [ index ]; res.send( JSON.stringify ( result )); } catch (e){ result.error = e.toString(); res.send(JSON.stringify(result)); } }); api.listen( 80 , function () { console.log( 'Zombie api listening on port 80!' ); }); var express = require( 'express' ); var api = express(); var zombies = [ {id: 1 , name: ' 瘋狂傑克 ' , age: 3.3 , isVegetarian: true , typeImgSrc: "z1.png" }, {id: 2 , name: ' 懶懶瑪琍 ' , age: 10 , isVegetarian: true , typeImgSrc: "z2.png" }, {id: 3 , name: ' ⼤提姆 ' , age: 5 , isVegetarian: false , typeImgSrc: "z3.png" }, {id: 4 , name: ' 史提夫 ' , age: 2 , isVegetarian: false , typeImgSrc: "z4.png" }, { id : 5 , name: ' 功夫李 ' , age: 1 , isVegetarian: true , typeImgSrc: "z5.png" } ] ; var maxID = 5 ; // 略過⼀些程式 api. post ( '/api/ deleteZombie ' , express.text(), function (req, res) { var result = { data: null , error: null }; try { let zombie = JSON.parse ( req.body ); if ( typeof zombie.id != 'number' || zombies.map (x => x. id ). indexOf (zombie.id) == - 1 ){ result.error = " 請輸入有效的 id " ; res.send(JSON.stringify(result)); return ; } let index = zombies.map(x => x.id). indexOf(zombie.id) ; let deletedZombie = zombies [ index ]; zombies . splice ( index , 1 ); result.data = deletedZombie ; res.send( JSON.stringify ( result )); } catch (e){ result.error = e.toString(); res.send(JSON.stringify(result)); } }); api.listen( 80 , function () { console.log( 'Zombie api listening on port 80!' ); }); console.log( typeof 10.123 ); var num1 = parseFloat ( ' 10.123 ' ); console.log( typeof num1 ); var z1 = { id: 1 , name: ' 瘋狂傑克 ' , age: 3.3 , isVegetarian: true , typeImgSrc: “z1.png" } ; var str1 = JSON . stringify ( z1 ); console.log(str1); console.log( typeof str1 ); var str2 = ' {"id":1, "name":" 懶懶瑪琍 ", "age":10, "isVegetarian":true, "typeImgSrc": “z1.png"} ' ; var z2 = JSON.parse ( str2 ); console.log(z2); console.log( typeof z2 ); var names = [ ' 瘋狂傑克 ' , ' 懶懶瑪琍 ' , ' ⼤提姆 ' , ' 史提夫 ' , 功夫李 ' ] ; var hiNames = names . map ( x => 'hi~' + x ); console.log( hiNames );