溫馨提示×

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

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

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

發(fā)布時(shí)間:2021-09-24 09:45:44 來源:億速云 閱讀:167 作者:小新 欄目:web開發(fā)

小編給大家分享一下nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

基于node.js實(shí)現(xiàn)數(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)圖

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

實(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ù)庫

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

  • postman測(cè)試

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

  • 控制臺(tái)輸出結(jié)果

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

使用路由中間件優(yōu)化方案

思路

  • 創(chuàng)建項(xiàng)目

  • 初始化項(xiàng)目文件夾

npm init --y
  • 安裝包

npm i express mysql
  • restfulf 風(fēng)格

  • 使用Postman軟件測(cè)試

項(xiàng)目結(jié)構(gòu)圖

nodejs怎么添加和查詢數(shù)據(jù)庫的數(shù)據(jù)

實(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è)資訊頻道!

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

免責(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)容。

AI