1.新建app.js粘贴以下代码
2.npm init 初始化
3.npm i 安装依赖4.npm i mysql
5.npm i express
6. node app.js 启动接口
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)// 创建数据库链接
const connection = mysql.createConnection({host: 'localhost',user: 'root',password: 'root',database: 'mall'
})connection.connect()//设置跨域访问
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();
});//好物秒杀电梯导航
app.get('/elevator', (req, res) => {connection.query(`select * from index_all limit 0,168;`, function (err, rows, fields) {res.send({status: 200,data: rows})})
})// 加入购物车
app.post('/collect', (req, res) => {const user_id = req.body.userIdconst id = req.body.idconst count = req.body.countconnection.query(`insert into t_collections (user_id, commodity_id,count) values (${user_id},${id},${count});`, function (err, rows, fields) {res.send({status: 200})});
})// 更新购物车数量
app.get('/updataNum', (req, res) => {const userId = req.query.userIdconst commodity_id = req.query.commodity_idconnection.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})}})
})// 我的购物车
app.get('/car', (req, res) => {const id = req.query.idconnection.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})})
})// 查询注册的手机号
app.get("/seePhone", (req, res) => {const telnumber = req.query.telnumberconnection.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})}})
})// 查询购物车
app.get("/seeCar", (req, res) => {const user_id = req.query.user_idconst commodity_id = req.query.commodity_idconsole.log(777, user_id, commodity_id)// const count = req.query.countconnection.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})})
})// 修改购物车
app.put("/reviseCar", (req, res) => {const user_id = req.body.user_idconst commodity_id = req.body.commodity_idconst count = req.body.countconnection.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.get("/addCart", (req, res) => {const user_id = req.query.user_idconst commodity_id = req.query.commodity_idconst count = req.query.countconnection.query(`insert into t_collections (commodity_id,user_id,count) values (${commodity_id}, ${user_id},${count});`, function (err, rows, fields) {res.send({status: 200})})
})// 购物车 增加/删减 商品数量
app.put("/changeMount", (req, res) => {const user_id = req.body.user_idconst commodity_id = req.body.commodity_idconst count = req.body.countconnection.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_idconst commodity_id = req.query.commodity_idconnection.query(`delete from t_collections where user_id=${user_id} and commodity_id=${commodity_id};`, function (err, rows, fields) {res.send({status: 200})})
})//首页底部数据
app.get('/index', (req, res) => {const mount = req.query.limitconst page = req.query.pageconnection.query(`select * from index_index limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {res.send({status: 200,data: rows})})
})//ole大牌精选数据
app.get('/olee', (req, res) => {const mount = req.query.limitconst page = req.query.pageconnection.query(`select * from index_ole limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {res.send({status: 200,data: rows})})
})// 所有数据接口
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.limitconst page = req.query.pageconst txt = req.query.keywordconnection.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.keywordconnection.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.adminconst psw = req.query.pswconnection.query(`select * from t_users where telnumber='${admin}' and password='${psw}';`, function (err, rows, fields) {res.send({status: 200,data: rows})})
})//注册接口
app.post('/register', (req, res) => {console.log(req.body.telnumber)const telnumber = req.body.telnumberconst psw = req.body.passwordconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.query(`select * from index_makeups limit ${(page - 1) * mount},${mount};`, function (err, rows, fields) {res.send({status: 200,data: rows})})
})// 运动接口
app.get('/sports', (req, res) => {const mount = req.query.limitconst page = req.query.pageconnection.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.limitconst page = req.query.pageconnection.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}`)
})