您好,登錄后才能下訂單哦!
小編給大家分享一下nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
思路
創(chuàng)建項(xiàng)目 serverAPI
初始化項(xiàng)目文件夾
npm init --y
安裝包
npm i express mysql
restfulf 風(fēng)格
使用Postman軟件測(cè)試
【推薦學(xué)習(xí):《nodejs 教程》】
項(xiàng)目結(jié)構(gòu)圖
實(shí)現(xiàn)
sql.js
文件代碼如下:
// 1. 加載msyql var mysql = require('mysql'); // 2. 創(chuàng)建連接 var connection = mysql.createConnection({ host : 'localhost', // 你要連接的數(shù)據(jù)庫服務(wù)器的地址 port : 3306,// 端口號(hào) user : 'root', // 連接數(shù)據(jù)庫服務(wù)器需要的用戶名 password : 'root', // 連接數(shù)據(jù)庫服務(wù)器需要的密碼 database : 'yanyan' //你要連接的數(shù)據(jù)庫的名字 }); // 3. 連接數(shù)據(jù)庫 connection.connect((err) => { // 如果有錯(cuò)誤對(duì)象,表示連接失敗 if (err) return console.log('數(shù)據(jù)庫連接失敗') // 沒有錯(cuò)誤對(duì)象提示連接成功 console.log('mysql數(shù)據(jù)庫連接成功') }); module.exports = connection
server.js
文件 參考代碼
const express = require("express"); const app = express(); const connection = require("./utils/sql"); app.use(express.urlencoded()); // 添加數(shù)據(jù)接口 app.post("/api/student", (req, res) => { console.log(req.body); // 接收普通鍵值對(duì)參數(shù) const { name, sex, age } = req.body; // 添加到數(shù)據(jù)庫中 const sql = `insert into Students(name,sex,age) value('${name}','${sex}',${age})`; //console.log("要執(zhí)行的sql", sql); // result 接受的數(shù)據(jù) connection.query(sql, (err, result) => { if (err) { console.log(err); res.json({ msg: "添加失敗", code: 0 }); } else { console.log(result); res.json({ msg: "添加成功", code: 1 }); } }); }); // 獲取數(shù)據(jù)接口 app.get("/api/student", (req, res) => { const sql = `select * from Students `; connection.query(sql, (err, result) => { if (err) { console.log(err); res.json({ msg: "獲取失敗", code: 0 }); } else { console.log(result); res.json({ msg: "獲取成功", code: 0, data: result }); } }); }); app.listen(3000, () => { console.log("接口服務(wù)器啟動(dòng),端口號(hào)為3000"); });
運(yùn)行結(jié)果
sql數(shù)據(jù)庫
postman測(cè)試
控制臺(tái)輸出結(jié)果
思路
創(chuàng)建項(xiàng)目
初始化項(xiàng)目文件夾
npm init --y
安裝包
npm i express mysql
restfulf 風(fēng)格
使用Postman軟件測(cè)試
項(xiàng)目結(jié)構(gòu)圖
實(shí)現(xiàn)
sql.js
文件
// 1. 加載mysql var mysql = require("./node_modules/mysql"); // 2. 創(chuàng)建連接 var connection = mysql.createConnection({ host: "localhost", // 你要連接的數(shù)據(jù)庫服務(wù)器的地址 port: 3306, // 端口號(hào) user: "root", // 連接數(shù)據(jù)庫服務(wù)器需要的用戶名 password: "root", // 連接數(shù)據(jù)庫服務(wù)器需要的密碼 database: "yanyan", //你要連接的數(shù)據(jù)庫的名字 }); // 3. 連接數(shù)據(jù)庫 connection.connect((err) => { // 如果有錯(cuò)誤對(duì)象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗"); // 沒有錯(cuò)誤對(duì)象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功"); }); module.exports = connection;
get.js
文件
const connection = require("./sql"); const express = require("./node_modules/express"); const router = express.Router(); router.use(express.urlencoded()); //獲取數(shù)據(jù)接口 router.get("/api/student", (req, res) => { const sql = `select * from Students`; connection.query(sql, (err, result) => { if (err) { console.log(err); res.json({ msg: "獲取失敗", code: 0 }); } else { console.log(result); res.json({ msg: "獲取成功", code: 0, data: result }); } }); }); module.exports = router;
post.js
文件
const connection = require("./sql"); const express = require("./node_modules/express"); const router = express.Router(); router.use(express.urlencoded()); // 添加數(shù)據(jù)接口 router.post("/api/student", (req, res) => { //console.log(req.body); // 接收普通鍵值對(duì)參數(shù) const { name, sex, age } = req.body; // 添加到數(shù)據(jù)庫中 const sql = `insert into Students(name,sex,age) values('${name}','${sex}',${age})`; //console.log("要執(zhí)行的sql", sql); // result 接受的數(shù)據(jù) connection.query(sql, (err, data) => { if (err) { console.log(err); res.json({ msg: "添加失敗", code: 0 }); } else { console.log(data); res.json({ msg: "添加成功", code: 1 }); } }); }); module.exports = router;
server-pro.js
文件
const get = require("./utils/get"); const post = require("./utils/post"); const express = require("./node_modules/express"); const app = express(); app.use("/utils/get", get); app.use("/utils/post", post); app.listen(3000, () => { console.log("接口服務(wù)器啟動(dòng),端口號(hào)為3000"); });
以上是“nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。