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

相关推荐
天天进步20153 天前
Node.js中Express框架入门教程
node.js·express
mosen8683 天前
易混淆的CommonJS和ESM(ES Module)及它们区别
javascript·node.js·express
一枚小小程序员哈8 天前
基于Vue + Node能源采购系统的设计与实现/基于express的能源管理系统#node.js
vue.js·node.js·express
一枚小小程序员哈8 天前
基于Vue的个人博客网站的设计与实现/基于node.js的博客系统的设计与实现#express框架、vscode
vue.js·node.js·express
茶茶只知道学习15 天前
Express中间件和路由及响应方法
中间件·express
计算机毕设定制辅导-无忧学长19 天前
InfluxDB 与 Node.js 框架:Express 集成方案(二)
node.js·express
啃火龙果的兔子21 天前
Node.js (Express) + MySQL + Redis构建项目流程
mysql·node.js·express
计算机毕设定制辅导-无忧学长23 天前
InfluxDB 与 Node.js 框架:Express 集成方案(一)
node.js·express
gongzemin25 天前
使用Node.js开发微信第三方平台后台
微信小程序·node.js·express
都给我1 个月前
服务器中涉及节流(Throttle)的硬件组件及其应用注意事项
服务器·网络·express