国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Node.js寫接口連接MySQL數(shù)據(jù)庫(kù)

這篇具有很好參考價(jià)值的文章主要介紹了Node.js寫接口連接MySQL數(shù)據(jù)庫(kù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1.新建app.js粘貼以下代碼

2.npm init 初始化
3.npm i 安裝依賴

4.npm i mysql

5.npm i express

6. node app.js 啟動(dòng)接口文章來源地址http://www.zghlxwxcb.cn/news/detail-605430.html

const express = require('express')
const mysql = require('mysql')
const bodyParser = require('body-parser')
const app = express()
const port = 3006
const jsonParser = bodyParser.json()
const urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(urlencodedParser)
app.use(jsonParser)


// 創(chuàng)建數(shù)據(jù)庫(kù)鏈接
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'mall'
})

connection.connect()

//設(shè)置跨域訪問
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
});

//好物秒殺電梯導(dǎo)航
app.get('/elevator', (req, res) => {
    connection.query(`select * from index_all limit 0,168;`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 加入購(gòu)物車
app.post('/collect', (req, res) => {
    const user_id = req.body.userId
    const id = req.body.id
    const count = req.body.count
    connection.query(`insert into t_collections (user_id, commodity_id,count) values (${user_id},${id},${count});`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    });
})

// 更新購(gòu)物車數(shù)量
app.get('/updataNum', (req, res) => {
    const userId = req.query.userId
    const commodity_id = req.query.commodity_id
    connection.query(`SELECT * FROM t_collections, index_all where t_collections.commodity_id = index_all.id and user_id = ${userId} and commodity_id = ${commodity_id}`, function (err, rows, fields) {
        if (rows.length != 0) {
            res.send({
                status: 200
            })
        } else {
            res.send({
                status: 666
            })
        }
    })
})

// 我的購(gòu)物車
app.get('/car', (req, res) => {
    const id = req.query.id
    connection.query(`SELECT * FROM t_collections,index_all where t_collections.commodity_id=index_all.id and user_id=${id}`, function (err, rows, fields) {
        console.log(rows)
        res.send({
            data: rows
        })
    })
})

// 查詢注冊(cè)的手機(jī)號(hào)
app.get("/seePhone", (req, res) => {
    const telnumber = req.query.telnumber
    connection.query(`SELECT * FROM t_users where telnumber=${telnumber};`, function (err, rows, fields) {
        console.log(rows)
        if (rows.length == 0) {
            res.send({
                status: 200
            })
        } else {
            res.send({
                status: 666
            })
        }
    })
})

// 查詢購(gòu)物車
app.get("/seeCar", (req, res) => {
    const user_id = req.query.user_id
    const commodity_id = req.query.commodity_id
    console.log(777, user_id, commodity_id)
    // const count = req.query.count
    connection.query(`SELECT count FROM t_collections where t_collections.commodity_id=${commodity_id} and user_id=${user_id};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 修改購(gòu)物車
app.put("/reviseCar", (req, res) => {
    const user_id = req.body.user_id
    const commodity_id = req.body.commodity_id
    const count = req.body.count
    connection.query(`update t_collections set count=${count} where commodity_id=${commodity_id} and user_id=${user_id};`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    })
})

// 添加購(gòu)物車
app.get("/addCart", (req, res) => {
    const user_id = req.query.user_id
    const commodity_id = req.query.commodity_id
    const count = req.query.count
    connection.query(`insert into t_collections (commodity_id,user_id,count) values (${commodity_id}, ${user_id},${count});`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    })
})

// 購(gòu)物車 增加/刪減 商品數(shù)量
app.put("/changeMount", (req, res) => {
    const user_id = req.body.user_id
    const commodity_id = req.body.commodity_id
    const count = req.body.count
    connection.query(`update t_collections set count=${count} where commodity_id=${commodity_id} and user_id=${user_id};`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    })
})

//刪除商品
app.delete('/del', (req, res) => {
    const user_id = req.query.user_id
    const commodity_id = req.query.commodity_id
    connection.query(`delete from t_collections where user_id=${user_id} and commodity_id=${commodity_id};`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    })
})

//首頁(yè)底部數(shù)據(jù)
app.get('/index', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_index limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//ole大牌精選數(shù)據(jù)
app.get('/olee', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_ole limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})


// 所有數(shù)據(jù)接口
app.get('/all', (req, res) => {
    connection.query('select * from index_all', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 模糊搜索
app.get('/search', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    const txt = req.query.keyword
    connection.query(`select * from index_all  where title like "%${txt}%" limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 模糊搜索的所有商品
app.get('/allsearchh', (req, res) => {
    const txt = req.query.keyword
    connection.query(`select * from index_all where title like "%${txt}%";`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//登錄接口
app.get('/login', (req, res) => {
    const admin = req.query.admin
    const psw = req.query.psw
    connection.query(`select * from t_users where telnumber='${admin}' and password='${psw}';`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//注冊(cè)接口
app.post('/register', (req, res) => {
    console.log(req.body.telnumber)
    const telnumber = req.body.telnumber
    const psw = req.body.password
    connection.query(`insert into t_users (password,telnumber) values ('${psw}','${telnumber}');`, function (err, rows, fields) {
        res.send({
            status: 200
        })
    })
})

// 更多接口 
app.get('/more', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_more limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 百貨接口 
app.get('/store', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_store limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 電器接口 
app.get('/appliances', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_appliances limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//男裝接口 
app.get('/man', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_man limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 母嬰接口 
app.get('/baby', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_baby limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 箱包接口 
app.get('/bag', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_bag limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 美妝接口 
app.get('/makeups', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_makeups limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 運(yùn)動(dòng)接口  
app.get('/sports', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_sports limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 女裝接口 
app.get('/woman', (req, res) => {
    const mount = req.query.limit
    const page = req.query.page
    connection.query(`select * from index_woman limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 大牌精選接口
app.get('/good', (req, res) => {
    connection.query('select * from index_good', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 好物秒殺接口
app.get('/appliances', (req, res) => {
    connection.query('select * from index_appliances', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//瘋狂折扣接口
app.get('/appliances', (req, res) => {
    connection.query('select * from index_appliances', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

//推薦接口
app.get('/appliances', (req, res) => {
    connection.query('select * from index_appliances', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

// 瘋狂接口1~6
app.get('/crazy1', (req, res) => {
    connection.query('select * from crazy1', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/crazy2', (req, res) => {
    connection.query('select * from crazy2', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/crazy3', (req, res) => {
    connection.query('select * from crazy3', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/crazy4', (req, res) => {
    connection.query('select * from crazy4', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/crazy5', (req, res) => {
    connection.query('select * from crazy5', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/crazy6', (req, res) => {
    connection.query('select * from crazy6', function (err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

到了這里,關(guān)于Node.js寫接口連接MySQL數(shù)據(jù)庫(kù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 前端使用node.js連接sql.server數(shù)據(jù)庫(kù)教程

    前端使用node.js連接sql.server數(shù)據(jù)庫(kù)教程

    最近項(xiàng)目中要用到node寫接口然后連接公司現(xiàn)有的sql.server數(shù)據(jù)庫(kù),再把執(zhí)行結(jié)果返回給前端(還是我),因?yàn)橹耙恢弊銮岸诉@塊,后端這方面不是很懂,花了很長(zhǎng)的時(shí)間終于研究出來了(還是太菜了,走了很多彎路),所以寫個(gè)博客,一是復(fù)習(xí)鞏固,二是給其他有需要的小伙伴一個(gè)參考,盡量

    2024年02月11日
    瀏覽(27)
  • 在Node.js中使用MongoDB連接數(shù)據(jù)庫(kù)、創(chuàng)建集合

    在Node.js中使用MongoDB連接數(shù)據(jù)庫(kù)、創(chuàng)建集合

    本文主要介紹在Node.js中使用MongoDB連接數(shù)據(jù)庫(kù)、創(chuàng)建集合的方法。 在Node.js中使用MongoDB連接數(shù)據(jù)庫(kù)有兩種方式:使用原生驅(qū)動(dòng)程序和使用Mongoose。 首先,需要安裝 mongodb 模塊??梢酝ㄟ^以下命令來安裝: 安裝完成后,可以在代碼中引入模塊: 接著,可以使用 MongoClient 來連接

    2024年02月04日
    瀏覽(82)
  • Node.js程序如何訪問MySQL數(shù)據(jù)庫(kù)呢?Sequelize操作MySQL數(shù)據(jù)庫(kù)詳解

    當(dāng)我們安裝好MySQL后,Node.js程序如何訪問MySQL數(shù)據(jù)庫(kù)呢? 訪問MySQL數(shù)據(jù)庫(kù)只有一種方法,就是通過網(wǎng)絡(luò)發(fā)送SQL命令,然后,MySQL服務(wù)器執(zhí)行后返回結(jié)果。 我們可以在命令行窗口輸入mysql -u root -p,然后輸入root口令后,就連接到了MySQL服務(wù)器。因?yàn)闆]有指定–host參數(shù),所以我們連

    2023年04月08日
    瀏覽(30)
  • Vue項(xiàng)目通過node連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)增刪改查操作

    Vue項(xiàng)目通過node連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)增刪改查操作

    1.創(chuàng)建Vue項(xiàng)目 Vue項(xiàng)目創(chuàng)建的詳細(xì)步驟,有需要的可移步這里 2.下載安裝需要的插件 下載express 下載cors,用于處理接口跨域問題 下載mysql 下載axios 3.在項(xiàng)目中創(chuàng)建server文件夾,用于搭建本地服務(wù)器 新建/server/app.js,用于配置服務(wù)器相關(guān)信息 新建/server/db/index.js,用于配置數(shù)據(jù)庫(kù)

    2024年02月16日
    瀏覽(29)
  • MySQL數(shù)據(jù)庫(kù) – node使用

    MySQL數(shù)據(jù)庫(kù) – node使用

    1 MySQL查詢對(duì)象 2 MySQL查詢數(shù)組 3 mysql2庫(kù)介紹使用 4 mysql2預(yù)處理語(yǔ)句 5 mysql2連接池使用 6 mysql2的Promi 這里僅說明如何使用服務(wù)器連接數(shù)據(jù)庫(kù)并進(jìn)行操作。 預(yù)處理語(yǔ)句就是可以輸入變量的語(yǔ)句(表現(xiàn)形式是有符號(hào):?)。需要使用.execute來執(zhí)行; ? 需要運(yùn)行普通的語(yǔ)句(不添加變

    2024年02月08日
    瀏覽(25)
  • Mongodb 以及 node.js中使用mongoose操作數(shù)據(jù)庫(kù)

    目錄 1、lowdb 2、Mongodb是什么? 3、Mongodb核心概念 4、Mongodb的下載與使用 5、數(shù)據(jù)庫(kù)與集合命令 5.1、數(shù)據(jù)庫(kù)命令 5.2、集合命令 5.3、文檔命令 6、Mongoose 6.1、插入文檔 6.2、字段類型 6.3、字段值驗(yàn)證 6.3.1、必填項(xiàng) 6.3.2、默認(rèn)值 6.3.3、枚舉值 6.3.4、唯一值 6.4、刪除文檔 6.5、更新文

    2024年02月11日
    瀏覽(28)
  • Node框架 【Koa】之 【靜態(tài)資源管理、模板引擎、連接數(shù)據(jù)庫(kù)】
  • Nodejs 入門8 NeDB 輕量級(jí)的Node.js 數(shù)據(jù)庫(kù)

    Nodejs 入門8 NeDB 輕量級(jí)的Node.js 數(shù)據(jù)庫(kù)

    常用的sqlite輕量級(jí)數(shù)據(jù)庫(kù),nodejs在windows環(huán)境下安裝配置有時(shí)候會(huì)比較麻煩,很難順利安裝。 Nedb(Node Embedded Database)在一些情況下可以替代sqlite,特別適用于小型項(xiàng)目和快速原型開發(fā)。本文將介紹Nedb的基本概念、特性和使用方法,以幫助大家更好地了解和利用這個(gè)便捷的工

    2024年01月22日
    瀏覽(30)
  • 自學(xué)WEB后端05-Node.js后端服務(wù)鏈接數(shù)據(jù)庫(kù)redis

    自學(xué)WEB后端05-Node.js后端服務(wù)鏈接數(shù)據(jù)庫(kù)redis

    嘿,親愛的小伙伴們!?? 今天我要給大家分享一個(gè)超級(jí)方便且高效的 NoSQL 類型數(shù)據(jù)庫(kù)——Redis!?? 它可不是一般的關(guān)系型數(shù)據(jù)庫(kù)哦,而是以鍵值對(duì)形式存儲(chǔ)數(shù)據(jù)的內(nèi)存數(shù)據(jù)庫(kù)。?? 快跟著我一起來學(xué)習(xí)如何安裝和使用 Redis 吧!? 自學(xué)WEB后端01-安裝Express+Node.js框架完成Hello

    2024年02月07日
    瀏覽(34)
  • 【接口測(cè)試】Postman 連接數(shù)據(jù)庫(kù)MySQL

    【接口測(cè)試】Postman 連接數(shù)據(jù)庫(kù)MySQL

    ?平常用postman測(cè)試接口時(shí),可能會(huì)寫死某些值,不太靈活。 有沒有辦法利用數(shù)據(jù)庫(kù)的數(shù)據(jù)動(dòng)態(tài)測(cè)試呢?答案當(dāng)然是可以的。 可以使用Postman連接MySQL,因?yàn)閜ostman可以執(zhí)行js,所以搭配node服務(wù)就可以實(shí)現(xiàn)。 node環(huán)境 地址:https://github.com/liyinchigithub/Omysql ?當(dāng)然也可以用docker安裝,

    2024年02月12日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包