移动校园(2):express构建服务器,小程序调用接口,展示数据

发布于:2024-07-07 ⋅ 阅读:(37) ⋅ 点赞:(0)

express做服务器框架,mssql连接数据库,uni-request调用接口

这是文件夹目录

然后是index.js内容

const express=require('express')
const app=express()
const uniRouter=require("./uniRouter")
const config={
    user:'sa',
    password:'123456',
    server:'localhost',
    database:'uniSchool',
    port: 1433,
    encrypt: false, 
    pool: {
        max: 20,
        min: 0,
        idleTimeoutMillis: 30000
    }
}
const sql=require('mssql')
const pool=new sql.ConnectionPool(config)
pool.connect().then((pool)=>
{
    app.locals.db=pool;
    const server=app.listen(3090,()=>
    {
        console.log(`正在监听${server.address().port}`)
    })
})
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});
app.use('/uni',uniRouter)
app.get('*',(req,res)=>
{
    res.send('网址输错了,请修改')
})
const express=require("express")
const router=express.Router()
const sql=require('mssql')

//点击精准查询

//全部地物
router.get('/builds',async function(req,res)
    {
        let re=await req.app.locals.db.query('select * from building')
        res.send(re.recordset)
    }
)
//分类
router.get('/store',async function(req,res)
    {
        let re=await req.app.locals.db.query("select * from building where id in ('6827585794865333373','4094331145955984864','5703449142701393167')")
        res.send(re.recordset)
    }
)
router.get('/home',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where category='房产小区:房产小区附属'")
    res.send(re.recordset)
})
router.get('/run',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where category like'运动健身%'")
    res.send(re.recordset)
})
router.get('/teach',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where category = '教育学校:教育学校附属'")
    res.send(re.recordset)
})
router.get('/food',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where category ='食堂'")
    res.send(re.recordset)
})
router.get('/hos',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where id ='5900375277262852961'")
    res.send(re.recordset)
})
router.get('/library',async function(req,res)
{
    let re=await req.app.locals.db.query("select * from building where category ='文化场馆:图书馆'")
    res.send(re.recordset)
})


//模糊匹配查询
//只返回五条
router.get('/search/:keyword',async function(req,res)
{
    let {keyword}=req.params
    let re=await req.app.locals.db.query(`select * from building where title like '%${keyword}%'`)
    res.send(re.recordset.slice(0, 5))
})
module.exports=router

然后是uni-app部分成果展示


网站公告

今日签到

点亮在社区的每一天
去签到