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

相关推荐
低代码布道师35 分钟前
第五部分:第五节 - Express 路由与中间件进阶:厨房的分工与异常处理
中间件·express
码农捻旧2 天前
解决Mongoose “Cannot overwrite model once compiled“ 错误的完整指南
javascript·数据库·mongodb·node.js·express
HWL56793 天前
Express项目解决跨域问题
前端·后端·中间件·node.js·express
程序员拂雨6 天前
Express知识框架
node.js·express
layman05288 天前
node.js 实战——餐厅静态主页编写(express+node+ejs+bootstrap)
node.js·bootstrap·express
layman05289 天前
node.js 实战——express图片保存到本地或服务器(七牛云、腾讯云、阿里云)
node.js·express
小妖66612 天前
express 怎么搭建 WebSocket 服务器
websocket·网络协议·express
一袋米扛几楼9819 天前
【前端】从零开始的搭建顺序指南(技术栈:Node.js + Express + MongoDB + React)book-management
前端·node.js·express
layman052820 天前
node.js 实战——从0开始做一个餐厅预订(express+node+ejs+bootstrap)
node.js·express
白昼的星光@20 天前
使用nodeJs的express+axios+cors做代理
express