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

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

这是文件夹目录

然后是index.js内容

javascript 复制代码
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('网址输错了,请修改')
})
javascript 复制代码
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部分成果展示

相关推荐
weixin79893765432...1 天前
Vue + Express + DeepSeek 实现一个简单的对话式 AI 应用
vue.js·人工智能·express
D***y2013 天前
使用Node.js搭配express框架快速构建后端业务接口模块Demo
node.js·express
q***4646 天前
使用Node.js搭配express框架快速构建后端业务接口模块Demo
node.js·express
T***16078 天前
ConfigMap:解耦应用配置的利器
hdfs·ruby·express
java_logo9 天前
GPUSTACK Docker 容器化部署指南
运维·mongodb·docker·云原生·容器·eureka·express
java_logo11 天前
MONGO-EXPRESS Docker 容器化部署指南
linux·运维·mongodb·docker·容器·express
天蓝色的鱼鱼13 天前
前端小白Express入门:初识Web框架与项目搭建
前端·node.js·express
k***858413 天前
使用Node.js搭配express框架快速构建后端业务接口模块Demo
node.js·express
q***05614 天前
使用Node.js搭配express框架快速构建后端业务接口模块Demo
node.js·express
2503_9284115614 天前
11.11 Express-generator和文件上传和身份认证
数据库·node.js·express