日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql建表的auto_increment_koa2+koa+mysql快速搭建nodejs服务器

發布時間:2025/3/21 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql建表的auto_increment_koa2+koa+mysql快速搭建nodejs服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用koa的腳手架koa-generator可以快速生成項目骨架,可以用于發開或者測試接口
https://github.com/hellojinjin123/node-koa2-template

1. 全局安裝koa-generator(不用全局安裝koa)

項目名字fast-koa

npm install koa-generator -gkoa2 fast-koacd fast-koanpm install

目錄結構如下
-bin // www 項目啟動目錄 node ./www
-public // 靜態網站放置目錄 也就是vue dist代碼放置的地 項目入口index.html
-routes // 路由
-views // 視圖 服務器渲染使用的模板
-app.js // 項目入口
-packaga.json

2. 啟動項目

// package.json "scripts": { "start": "node bin/www", "dev": "./node_modules/.bin/nodemon bin/www", "prd": "pm2 start bin/www", "test": "echo "Error: no test specified" && exit 1" }

運行npm run dev開啟服務器
同時可以看到generator自帶了nodemon(Nodemon 是一款非常實用的工具,用來監控你 node.js 源代碼的任何變化和自動重啟你的服務器)
如下圖:服務器啟動了

3. 項目入口app.js

// app.jsconst Koa = require('koa')const app = new Koa()const views = require('koa-views')const json = require('koa-json')const onerror = require('koa-onerror')const bodyparser = require('koa-bodyparser')const logger = require('koa-logger')const index = require('./routes/index')const users = require('./routes/users')// error handleronerror(app)// middlewaresapp.use(bodyparser({ enableTypes:['json', 'form', 'text']}))app.use(json())app.use(logger())app.use(require('koa-static')(path.resolve(__dirname, config.publicPath))))app.use(views(__dirname + '/views', { extension: 'pug'}))// loggerapp.use(async (ctx, next) => { const start = new Date() await next() const ms = new Date() - start console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)})// routesapp.use(index.routes(), index.allowedMethods())app.use(users.routes(), users.allowedMethods())// error-handlingapp.on('error', (err, ctx) => { console.error('server error', err, ctx)});module.exports = app

可以在根目錄路添加config.js 把一些公共的配置放入 比如數據庫信息,端口,靜態資源路徑等

// config.jsconst path = require('path');const config = { // 項目啟動監聽的端口 port: 3000, publicPath: 'public', logPath: 'logs/koa-template.log', // 數據庫配置 database: { HOST: 'xxx', // 數據庫地址 USERNAME: 'xxx', // 用戶名 PASSWORD: 'xxx', // 用戶密碼 DATABASE: 'xxx', // 數據庫名 PORT: 3306 // 數據庫端口(默認: 3306) }};module.exports = config;

4. koa-static 靜態資源中間件

app.use(require('koa-static')(path.resolve(__dirname, config.publicPath))))
koa-generator已經配置了靜態資源中間件,只要放入public目錄,靜態網站就可以運行
瀏覽http://localhost:3000/,服務器會優先讀取public下的index.html
如果沒有index.html,服務器會根據路由,判斷'/'是否有內容返回,沒有對應路由則返回404 not found
因為koa-generator默認設置了路由,所以服務器運行返回了Hello Koa 2!
如下:

router.get('/', async (ctx, next) => { await ctx.render('index', { title: 'Hello Koa 2!' })})

5. 添加models目錄

相當于服務器數據層,存放數據庫模型(相當于建表),使用Sequelize進行mysql操作

const { sequelize, Sequelize } = require('../config/db')const { DataTypes, Model } = Sequelizeclass Admin extends Model { /** * @description: 添加管理員 * @param {*} username * @param {*} password * @return {*} 返回添加的數據 */ static async createAdmin({ username, password }) { return await this.create({ username, password }) } /** * @description: 根據id修改管理員密碼 * @param {*} id * @return {*} 返回修改的數據 */ static async updatepwdById({id, password}) { return await this.update({ password }, { where: { id } }) } /** * @description: 根據id刪除管理員 * @param {*} id * @return {*} */ static async deleteById(id){ return await this.destroy({ where: { id } }) }}// 初始化表結構Admin.init( { id: { type: DataTypes.INTEGER, allowNull: false, //非空 autoIncrement: true, //自動遞增 primaryKey: true //主鍵 }, username: { type: DataTypes.STRING, field: "username", allowNull: false, unique: true // 唯一約束 用戶名不能重復 }, password: { type: DataTypes.STRING, allowNull: false }, active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true } }, { underscored: true, //額外字段以下劃線來分割 timestamps: true, //取消默認生成的createdAt、updatedAt字段 createdAt: "created_at", updatedAt: "updated_at", freezeTableName: true, // Model 對應的表名將與model名相同 comment: "管理員表類", // paranoid: true //虛擬刪除 sequelize, // 我們需要傳遞連接實例 // modelName: 'Admin', // 我們需要選擇模型名稱 // tableName: 'Admin' // 表名}) // 創建表格 ; (async () => { await Admin.sync(); console.log("Admin表剛剛(重新)創建!"); // 這里是代碼 })()// 定義的模型是類本身// console.log(User === sequelize.models.User); // truemodule.exports = Admin

6. mysql數據庫的使用(Sequelize stars 23.6k in github )

Sequelize 是一個基于 promise 的 Node.js ORM, 目前支持 Postgres, MySQL, MariaDB, SQLite 以及 Microsoft SQL Server. 它具有強大的事務支持, 關聯關系, 預讀和延遲加載,讀取復制等功能。

Sequelize 遵從 語義版本控制。 支持 Node v10 及更高版本以便使用 ES6 功能。https://www.sequelize.com.cn/core-concepts/model-basics

安裝mysql&sequelize

npm install --save mysql mysql2npm install --save sequelize

安裝sequelize之后,在config目錄下創建db.js,進行數據庫連接設置

const Sequelize = require('sequelize');const db = require('./index').db// 初始化數據庫const sequelize = new Sequelize(db.database, db.username, db.password, { host: db.host, dialect: 'mysql', pool: { max: 5, min: 0, idle: 10000 }})//測試數據庫鏈接sequelize.authenticate().then(function() { console.log("數據庫連接成功");}).catch(function(err) { //數據庫連接失敗時打印輸出 console.error(err); throw err;}); module.exports = { sequelize, Sequelize }

7. 添加controllers目錄

有了模型層對操作數據庫的支持,就可以進行業務操作了,也就是控制器目錄(在這個層可以放心調用models層方法進行curd)

// 導入模型const Admin = require('../models/admin')module.exports = { async getAllAdmins(ctx, next) { try { let data = await Admin.findAll() ctx.body = { msg: 1001, data } } catch (err) { ctx.body = { code: -1, msg: 1000, } } await next(); }, async createAdmin(ctx, next) { let req = ctx.request.body if (!req.username || !req.password) { ctx.body = { code: -1, msg: 1002 } return await next(); } try { let data = await Admin.createAdmin(req) ctx.body = { msg: 1003, data } } catch (err) { ctx.body = { code: -1, msg: 1000 } } await next(); }, async updatepwdById(ctx, next) { let req = ctx.request.body if (req.id && req.password) { try { await Admin.updatepwdById(req) ctx.body = { msg: 1004 } } catch (err) { ctx.body = { code: -1, msg: 1000 } } } else { ctx.body = { code: -1, msg: 1002 } } await next(); }, async deleteById(ctx, next) { let query = ctx.request.query // 獲取get請求參數 if (query && query.id) { try { await Admin.deleteById(query.id) ctx.body = { msg: 1005 } } catch (err) { ctx.body = { code: -1, msg: 1000 } } } else { ctx.body = { code: -1, msg: 1002 } } await next(); }}

8. 路由配置

// app.js 中添加// routesconst admin = require('./routes/admin')app.use(admin.routes(), admin.allowedMethods())

到此為止,一個完整的請求(接口)就處理完成了
比如請求 http://localhost:3000/admin/getAllAdmins

koa經歷的簡單過程:

  • 瀏覽器發出請求 -> 中間件 ->路由中間件 -> 中間件 -> 中間件若干回調 -> 瀏覽器收到響應
  • 路由:
    • router.get/post -> controllers.func -> models.func-> mysql
    • 請求行為 -> 業務邏輯 -> 模型支持 -> 入庫

    9. 配置自定義的中間件處理日志和response消息

    可以參考github代碼

    10. 附上一個很好理解的中間件原理的簡析

    // 根目錄下test.js// koa2 中間件原理簡析// 中間件的倉庫const arr = [ async (next) => { console.log(1); await next(); console.log(2); }, async (next) => { console.log(3); await new Promise((resolve, reject) => { setTimeout(() => { resolve( console.log(4) ); }, 3000); }); // 異步操作 await 會等待后面的promise resolve 后再向下執行 await next(); console.log(5); }, async (next) => { console.log(6); }, async (next) => { // 不會執行 因為上一個函數中沒有執行next console.log(7); await next(); console.log(8); }, async (next) => { // 不會執行 因為前面的函數中沒有執行next console.log(9); }];function fun(arr) { function dispose(index) { const currentFun = arr[index]; const next = dispose.bind(null, index + 1); return currentFun(next); // 尾遞歸 } dispose(0);}

    總結

    以上是生活随笔為你收集整理的mysql建表的auto_increment_koa2+koa+mysql快速搭建nodejs服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。