nodejs連接mysql數(shù)據(jù)庫及基本知識點(diǎn)詳解

本文實(shí)例講述了nodejs連接MySQL數(shù)據(jù)庫及基本知識點(diǎn)。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)袁州,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

一、幾個(gè)常用的全局變量

1、__filename獲取當(dāng)前文件的路徑
2、__dirname獲取當(dāng)前文件的目錄
3、process.cwd()獲取當(dāng)前工程的目錄

二、文件的引入與導(dǎo)出

1、使用require引入文件

2、使用module.exports導(dǎo)出文件中指定的變量、方法、對象

三、node項(xiàng)目的搭建目錄結(jié)構(gòu)

demo

    package.json 當(dāng)前項(xiàng)目所依賴的包或者模塊
    router 存放路由的文件
    views  存放視圖的模塊
    public 靜態(tài)文件
    module書寫模塊比如數(shù)據(jù)庫
    app.js主入口文件

四、將路由視圖單獨(dú)寫在router文件中demo

1、視圖視圖文件

const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
 res.send("hello word");
});
router.get("/article", (req, res) => {
 res.send("我是文章列表");
})
module.exports = router;

2、在主文件中調(diào)用

'use strict';
const express = require("express");
const app = express();
app.use("/",require("./router/03_router"))
app.use("/app",require("./router/03_router1"))
app.listen(3000);

五、使用ejs模板

1、需要安裝但可以不引入

npm install ejs --save

2、在主文件中配置

//配置模板的文件路徑
app.set("views",__dirname+"/views");
//配置模板引擎
app.set("view engine","ejs");

3、使用

①、模板文件

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport"
   content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
</head>
<body>
<h2>我是模板渲染的</h2>
</body>
</html>

②、在路由中渲染模板

'use strict';
const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
 //可以直接使用res.render("03_index");
 res.render("03_index.ejs");
});
router.get("/article", (req, res) => {
 res.send("我是文章列表");
})
module.exports = router;

③、主文件

'use strict';
const express = require("express");
const app = express();
//配置模板的文件路徑
app.set("views",__dirname+"/views");
//配置模板引擎
app.set("view engine","ejs");
app.use("/",require("./router/03_router"))
app.use("/app",require("./router/03_router1"))
app.listen(3000);

六、關(guān)于ejs模板文件的使用

1、返回?cái)?shù)據(jù)

...
let dataset = {
 name:"張三",
 age:20,
 books:['三國演義','西游記','紅樓夢','水滸傳']
}
res.render("03_index.ejs",dataset);
...

2、普通的字段

<h3><%= name %></h3>
<h3><%= age %></h3>

3、迭代數(shù)組

<ul>
 <% for(let i in books){%>
  <li><%= books[i] %></li>
 <%}%>
</ul>

七、加載靜態(tài)文件

1、主文件中配置

//設(shè)置靜態(tài)文件的加載(js,css,img)
app.use(express.static(__dirname+"/public"));

2、在模板中使用

<link rel="stylesheet" href="./css/bootstrap.css" rel="external nofollow" >
<script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script>
<img src="./img/002.jpg">
...

八、使用mysql數(shù)據(jù)庫

1、在module中創(chuàng)建一個(gè)db.js的文件

'use strict';
const mysql = require("mysql");
/**
 * 將整個(gè)方法全部暴漏出去
 * @param sql sql語句
 * @param arg 傳遞到sql語句中的參數(shù),可以不寫
 * @param callback 回調(diào)函數(shù),可以不寫
 */
module.exports = function (sql,arg,callback) {
 //1.創(chuàng)建連接(根據(jù)自己的數(shù)據(jù)庫配置)
 let config = mysql.createConnection({
  host:"localhost", //數(shù)據(jù)庫的地址
  user:"root", //數(shù)據(jù)庫用戶名
  password:"root", //數(shù)據(jù)庫密碼
  port:"3306", //mysql數(shù)據(jù)庫的端口號
  database:"mybatistest" //使用那個(gè)數(shù)據(jù)庫
 });
 //2.開始連接數(shù)據(jù)庫
 config.connect();
 //3.對數(shù)據(jù)庫的增刪改查操作
 config.query(sql,arg,(err,data)=>{
  callback && callback(err,data);
 })
 //4.關(guān)閉數(shù)據(jù)庫
 config.end();
}

2、在router視圖中使用查詢數(shù)據(jù)

①、引入文件

//引入數(shù)據(jù)庫文件
const db = require("./../module/db");

②、視圖中使用

router.get("/", (req, res) => {
 db("select * from m_dept",(err,data)=>{
  console.log(data);
  res.render("03_index.ejs",{data:data});
 })
});

3、新增數(shù)據(jù)

①、前端頁面見代碼案例

②、通過req.query獲取用戶數(shù)據(jù)參數(shù)

router.get("/regist",(req, res)=>{
 //獲取到輸入?yún)?shù),前提是input上要寫name
 console.log(req.query);
 db("insert into student(name,age) values(?,?)",[req.query.username,req.query.age],(err,data)=>{
  console.log(data);
  if(data){
   res.send("成功");
  }
 })
})

九、關(guān)于node返回json的方式

在前后端分離開發(fā)模式中后端返回的數(shù)據(jù)一般都是json,不需要使用ejs模板引擎了

...
res.json({
 info:"成功",
 code:1
});
...

十、github上的本章節(jié)代碼案例https://github.com/kuangshp/node-pro1

希望本文所述對大家nodejs程序設(shè)計(jì)有所幫助。

網(wǎng)頁題目:nodejs連接mysql數(shù)據(jù)庫及基本知識點(diǎn)詳解
轉(zhuǎn)載來于:http://bm7419.com/article44/pssehe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站網(wǎng)站改版、ChatGPT、小程序開發(fā)、Google網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化