1.新建app.js粘貼以下代碼
2.npm init 初始化
3.npm i 安裝依賴4.npm i mysql
5.npm i express文章來源:http://www.zghlxwxcb.cn/news/detail-605430.html
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)!