欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

node.js通過Sequelize 連接MySQL的方法

 更新時(shí)間:2020年12月28日 14:11:20   作者:ky_xin  
這篇文章主要介紹了node.js通過Sequelize 連接MySQL的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一.通過koa2腳手架構(gòu)建項(xiàng)目

1.1 安裝koa-generator
在終端輸入:

$ npm install -g koa-generator

1.2 使用koa-generator生成koa2項(xiàng)目

$ koa2 HelloKoa2

成功創(chuàng)建項(xiàng)目后,進(jìn)入項(xiàng)目目錄,并執(zhí)行npm install命令

$ cd HelloKoa2 
$ npm install

1.3 啟動(dòng)項(xiàng)目
在終端輸入:

$ npm start

項(xiàng)目啟動(dòng)后,默認(rèn)端口號(hào)是3000,在瀏覽器中運(yùn)行可以得到下圖的效果說明運(yùn)行成功。

二.創(chuàng)建連接

2.1剛剛創(chuàng)建的文件使用webstorm打開
新建一個(gè)db目錄

在這里插入圖片描述

2.2查看Sequelize文檔
使用npm安裝Sequelize

npm install --save sequelize

你還必須手動(dòng)為所選數(shù)據(jù)庫安裝驅(qū)動(dòng)程序選擇一個(gè)方法之一:

# 選擇以下之一:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server

我這里下載得是MySQL2

2.3連接數(shù)據(jù)庫
再剛剛創(chuàng)建得db文件加里面添加**config.js**
添加連接代碼:

module.exports = {
 dbsMysql: 'mysql://root:123456@localhost:3306/new'
 //root是數(shù)據(jù)庫管理員賬號(hào),‘123546'是密碼 3306是端口號(hào)(MySQL默認(rèn)是3306) school_admin是數(shù)據(jù)庫名稱

}

繼續(xù)在db文件夾里面添加mysql.js
添加連接以及添加日記:

const Sequelize = require('sequelize');
const mysqlurl = require('./config').dbsMysql
const sequelize = new Sequelize(mysqlurl, {
 // 選擇一種日志記錄參數(shù)
 logging: console.log // 默認(rèn)值,顯示日志函數(shù)調(diào)用的第一個(gè)參數(shù)
});
// //每次啟動(dòng)server刷新數(shù)據(jù)庫
//  (async ()=>{
//   await sequelize.sync({ force: true });
//  })()

module.exports = sequelize

三.創(chuàng)建模型

3.1模型定義
在db目錄下添加models文件夾再添加一個(gè)new2.js
定義模型:

const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = require('../mysql');

const new2 = sequelize.define('t_new2', {
  name: {
   type: DataTypes.STRING,
   allowNull: false
  },
 },
 {
  // 這是其他模型參數(shù)
  freezeTableName: true
 });
// 定義的模型是類本身
module.exports= new2

四.添加路由

4.1創(chuàng)建new2路由
在routes文件夾中添加new2.js

//引入kob得routes模塊
const router = require('koa-router')()
//定義模型為剛剛創(chuàng)建得new2.js
let Model = require("../db/models/new2");
//正常來說啟動(dòng)端口為http://localhost:3000 添加/new2就可以進(jìn)入new2路由
router.prefix('/new1')
// 進(jìn)入new2路由以后可以打印this is a users response!
router.get('/', function (ctx, next) {
 ctx.body = 'this is a users response!'

})
//設(shè)置增加add接口
router.post('/add', async function (ctx, next) {
 console.log(ctx.request.body)
 const new2 = await Model.create(ctx.request.body);
 ctx.body = {
  code:200,
  data:new2
 }
})
//設(shè)置查詢find接口
router.post('/find', async function (ctx, next) {
 const new2 =await Model.findAll({include: []})
 console.log(1111)
 ctx.body = {
  code: 200,
  data: new2
 }
})
//設(shè)置通過id得到所需信息的get接口
router.post('/get', async function (ctx, next) {
 // let users = await User.
 // find({})
 console.log(ctx.request.body)


 let new2 = await Model.findOne({
  // attributes: ['name', 'where']
  where: {
   id: ctx.request.body.id
  }
 });
 ctx.body = {
  code:200,
  data:new2
 }
})
//設(shè)置修改update接口
router.post('/update', async function (ctx, next) {
 console.log(ctx.request.body)
 // let pbj = await Model.update({ _id: ctx.request.body._id }, ctx.request.body);

 let new2 = await Model.update(ctx.request.body, {
  where: {
   id: ctx.request.body.id
  }
 });
 ctx.body = new2
})
//設(shè)置刪除delete接口
router.post('/delete', async function (ctx, next) {
 console.log(ctx.request.body)
 // 刪除所有名為 "Jane" 的人
 await Model.destroy({
  where: { id: ctx.request.body.id }
 });
 ctx.body = 'shibai '
})

// //每次啟動(dòng)server刷新數(shù)據(jù)庫
//  (async ()=>{
//   await sequelize.sync({ force: true });
//  })()
module.exports = router

4.2在app.js里面添加路由

在
在這里插入圖片描述

//引入剛剛創(chuàng)建的new2路由
const new2 =require('./routes/new2')
//使用我們的路由
app.use(new2.routes(),new2.allowedMethods())

4.3啟動(dòng)項(xiàng)目

在這里插入圖片描述

在數(shù)據(jù)庫中查看

在這里插入圖片描述

5.測試

5.1使用瀏覽器查看

輸入url:http://localhost:3000/new2

在這里插入圖片描述

5.2.使用postman測試接口
測試find接口(因?yàn)槲覀儗懙膄ind方法使用的post方法所以記得將get換成post):

http://localhost:3000/new2/find

在這里插入圖片描述

測試get接口

在這里插入圖片描述

展示一下最后的目錄

在這里插入圖片描述

到此這篇關(guān)于node.js通過Sequelize 連接MySQL的文章就介紹到這了,更多相關(guān)node.js連接MySQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • node+koa2+mysql+bootstrap搭建一個(gè)前端論壇

    node+koa2+mysql+bootstrap搭建一個(gè)前端論壇

    本篇文章通過實(shí)例給大家分享了用node+koa2+mysql+bootstrap搭建一個(gè)前端論壇的步驟,有需要的朋友參考下。
    2018-05-05
  • 詳解阿里Node.js技術(shù)文檔之process模塊學(xué)習(xí)指南

    詳解阿里Node.js技術(shù)文檔之process模塊學(xué)習(xí)指南

    這篇文章主要介紹了詳解阿里Node.js技術(shù)文檔之process模塊學(xué)習(xí)指南,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • NodeJs中express框架的send()方法簡介

    NodeJs中express框架的send()方法簡介

    這篇文章主要介紹了NodeJs中express框架的send()方法簡介,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • NodeJs安裝npm包一直失敗的解決方法

    NodeJs安裝npm包一直失敗的解決方法

    本篇文章主要介紹了NodeJs安裝npm包一直失敗的解決方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • node.js插件nodeclipse安裝圖文教程

    node.js插件nodeclipse安裝圖文教程

    這篇文章主要為大家分享了node.js插件nodeclipse安裝圖文教程,如何安裝node.js支持插件,下面小編為大家分享具體步驟
    2016-05-05
  • npm?install?404問題解決方案

    npm?install?404問題解決方案

    這篇文章主要給大家介紹了關(guān)于npm?install?404問題解決的相關(guān)資料,npm install命令既可以下載服務(wù)器上的模塊,也可以在本地創(chuàng)建自己的模塊,需要的朋友可以參考下
    2023-08-08
  • NestJS系列核心概念之Module模塊示例詳解

    NestJS系列核心概念之Module模塊示例詳解

    這篇文章主要為大家介紹了NestJS系列核心概念之Module模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Node定時(shí)備份MySQL的實(shí)現(xiàn)

    Node定時(shí)備份MySQL的實(shí)現(xiàn)

    本文主要介紹了Node定時(shí)備份MySQL的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Nodejs實(shí)現(xiàn)用戶注冊功能

    Nodejs實(shí)現(xiàn)用戶注冊功能

    本文通過實(shí)例代碼給大家介紹了Nodejs實(shí)現(xiàn)用戶注冊功能,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Node.js如何優(yōu)雅的封裝一個(gè)實(shí)用函數(shù)的npm包的方法

    Node.js如何優(yōu)雅的封裝一個(gè)實(shí)用函數(shù)的npm包的方法

    這篇文章主要介紹了Node.js如何優(yōu)雅的封裝一個(gè)實(shí)用函數(shù)的npm包的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論