移动校园(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部分成果展示

相关推荐
学习使我快乐012 天前
Express 学习
学习·node.js·express
Json____9 天前
node-电商商城平台实战项目(管理端+用户端)
node·vue2·express·element-ui·电商商城
星光开发者10 天前
基于springboot电动汽车租赁管理系统-计算机毕设 附源码 11217
javascript·spring boot·mysql·django·php·html5·express
一袋米扛几楼9815 天前
【报错问题】解决 Vercel 部署报错:Express 类型失效与 TypeScript 2349/2339/2769 错误排查
ubuntu·typescript·express
懒人村杂货铺16 天前
Express + TypeScript 后端通用标准规范
javascript·typescript·express
前端小超人rui17 天前
【Node.js Express中间件理解及中间件分类和作用】
中间件·node.js·express
前端小超人rui17 天前
封装Express 自定义中间件
中间件·node.js·express
Aolith17 天前
从前端模拟到全栈认证:我的论坛 JWT 实战复盘
node.js·express
Aolith20 天前
全栈论坛笔记:异步、HTTP 与安全基础
express
Aolith22 天前
学 Express 被 app.use 绕晕了?用流水线思维一次性搞懂 5 种中间件
后端·express