nodejs連接mysql數(shù)據(jù)庫(kù)及基本知識(shí)點(diǎn)詳解
本文實(shí)例講述了nodejs連接mysql數(shù)據(jù)庫(kù)及基本知識(shí)點(diǎn)。分享給大家供大家參考,具體如下:
一、幾個(gè)常用的全局變量
1、__filename獲取當(dāng)前文件的路徑
2、__dirname獲取當(dāng)前文件的目錄
3、process.cwd()獲取當(dāng)前工程的目錄
二、文件的引入與導(dǎo)出
1、使用require引入文件
2、使用module.exports導(dǎo)出文件中指定的變量、方法、對(duì)象
三、node項(xiàng)目的搭建目錄結(jié)構(gòu)
demo
package.json 當(dāng)前項(xiàng)目所依賴的包或者模塊
router 存放路由的文件
views 存放視圖的模塊
public 靜態(tài)文件
module 書寫模塊比如數(shù)據(jù)庫(kù)
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> <h1>我是模板渲染的</h1> </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:['三國(guó)演義','西游記','紅樓夢(mèng)','水滸傳']
}
res.render("03_index.ejs",dataset);
...
2、普通的字段
<h2><%= name %></h2> <h2><%= age %></h2>
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ù)庫(kù)
1、在module中創(chuàng)建一個(gè)db.js的文件
'use strict';
const mysql = require("mysql");
/**
* 將整個(gè)方法全部暴漏出去
* @param sql sql語(yǔ)句
* @param arg 傳遞到sql語(yǔ)句中的參數(shù),可以不寫
* @param callback 回調(diào)函數(shù),可以不寫
*/
module.exports = function (sql,arg,callback) {
//1.創(chuàng)建連接(根據(jù)自己的數(shù)據(jù)庫(kù)配置)
let config = mysql.createConnection({
host:"localhost", //數(shù)據(jù)庫(kù)的地址
user:"root", //數(shù)據(jù)庫(kù)用戶名
password:"root", //數(shù)據(jù)庫(kù)密碼
port:"3306", //mysql數(shù)據(jù)庫(kù)的端口號(hào)
database:"mybatistest" //使用那個(gè)數(shù)據(jù)庫(kù)
});
//2.開(kāi)始連接數(shù)據(jù)庫(kù)
config.connect();
//3.對(duì)數(shù)據(jù)庫(kù)的增刪改查操作
config.query(sql,arg,(err,data)=>{
callback && callback(err,data);
})
//4.關(guān)閉數(shù)據(jù)庫(kù)
config.end();
}
2、在router視圖中使用查詢數(shù)據(jù)
①、引入文件
//引入數(shù)據(jù)庫(kù)文件
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ù)
①、前端頁(yè)面見(jiàn)代碼案例
②、通過(guò)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的方式
在前后端分離開(kāi)發(fā)模式中后端返回的數(shù)據(jù)一般都是json,不需要使用ejs模板引擎了
...
res.json({
info:"成功",
code:1
});
...
十、github上的本章節(jié)代碼案例https://github.com/kuangshp/node-pro1
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
- 使用nodejs連接mySQL寫接口全過(guò)程(增刪改查)
- nodejs+mysql實(shí)現(xiàn)用戶相關(guān)的增刪改查的詳細(xì)操作
- 三分鐘教會(huì)你用nodejs操作mysql數(shù)據(jù)庫(kù)
- Nodejs?連接?mysql時(shí)報(bào)Error:?Cannot?enqueue?Query?after?fatal?error錯(cuò)誤的處理辦法
- NodeJs操作MYSQL方法詳細(xì)介紹
- nodejs中關(guān)于mysql數(shù)據(jù)庫(kù)的操作
- Nodejs中koa2連接mysql的實(shí)現(xiàn)示例
- NodeJs+MySQL實(shí)現(xiàn)注冊(cè)登錄功能
- nodejs實(shí)現(xiàn)的連接MySQL數(shù)據(jù)庫(kù)功能示例
- Nodejs連接mysql并實(shí)現(xiàn)增、刪、改、查操作的方法詳解
- NodeJS連接MySQL數(shù)據(jù)庫(kù)并進(jìn)行增刪改查操作詳解
相關(guān)文章
Node.js基于cors解決接口跨域的問(wèn)題(推薦)
這篇文章主要介紹了Node.js基于cors解決接口跨域的問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Node.js 使用流實(shí)現(xiàn)讀寫同步邊讀邊寫功能
本文通過(guò)代碼給大家介紹了Node.js 使用流實(shí)現(xiàn)讀寫同步邊讀邊寫功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的額朋友參考下吧2017-09-09
node.js中的fs.unlink方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.unlink方法使用說(shuō)明,本文介紹了fs.unlink的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
node.js博客項(xiàng)目開(kāi)發(fā)手記
本篇文章給大家總結(jié)了node.js博客項(xiàng)目開(kāi)發(fā)的相關(guān)步驟以及知識(shí)點(diǎn)分享,有興趣的朋友參考下。2018-03-03
Node.js 中的流Stream模塊簡(jiǎn)介及如何使用流進(jìn)行數(shù)據(jù)處理
Node.js中的流(Stream)模塊用于高效處理流式數(shù)據(jù),包括可讀流、可寫流、雙邊流和轉(zhuǎn)換流等,通過(guò)`fs.createReadStream`和`.pipe`方法可以方便地讀取文件并寫入控制臺(tái)或處理網(wǎng)絡(luò)請(qǐng)求,在實(shí)際開(kāi)發(fā)中,需要注意錯(cuò)誤處理、資源管理和性能優(yōu)化等問(wèn)題2025-03-03

