当前位置: 首页 > 后端技术 > Node.js

node.js与微信小程序后台数据库交互(五)给定表名查询数据

时间:2023-04-03 15:21:12 Node.js

终于写好了查询数据的步骤,前面的工作都是准备工作。查询页面page.html://page.htmlDocumentbody>提交page.js://page.jsvartest=document.getElementById('test');varbt=document.getElementById('bt');bt.onclick=function(){varstname=document.getElementById('tablename').value;//生成JSON字符串varvalue="{\"tablename\":\""+stname+"\"}";varxmlHttp=newXMLHttpRequest();xmlHttp.onreadystatechange=function(){if(xmlHttp.readyState==4&&xmlHttp.status==200){test.innerHTML=xmlHttp.responseText;varo=JSON.parse(xmlHttp.responseText);varsdata=(o.data)[0].toString();//data是一个数组,因为会返回多条记录varodata=JSON.parse(sdata);控制台日志(odata.distname);}};xmlHttp.open('POST','http://127.0.0.1:6060/',true);xmlHttp.setRequestHeader("Content-type","application/json;charset=UTF-8");xmlHttp.send(值);//对象转json};输入要查询的表名并提交node.js代码://query.jsconsthttp=require('http');constrequest=require('请求');varurltool=require('url');varfs=require('fs');//引入fs模块varaccessTokenJson=require('./wechat/access_token');//引入本地存储access_tokenconsthostIp='127.0.0.1';constapiPort=6060;constdata={appid:"wx4$%#%#%#",//你的微信小程序appidsecret:"@##¥¥...¥##R¥",//你微信小程序的appsecretgrant_type:"client_credential",env:"^%$#^@^"//你的微信小程序的环境参数};varexpress=require('express');varapp=express();varbodyParser=require('body-parser');app.使用(bodyParser.json());//用于解析application/json//允许自定义标头和CORSapp.all('*',function(req,res,next){res.header('Access-Control-Allow-Origin','*');res.header('Access-Control-Allow-Headers','Content-Type,Content-Length,Authorization,Accept,X-Requested-With,yourHeaderFeild');res.header('Access-Control-Allow-Methods','PUT,POST,GET,DELETE,OPTIONS');if(req.method=='OPTIONS'){res.sendStatus(200);/让选项请求快速返回/}else{next();}});app.post('/',function(req,res){getAccessToken(res);//处理微信小程序后台查询函数getCollectionFeedback(res,req);})functiongetAccessToken(res){//获取当前时间varcurrentTime=newDate().getTime();consturl='https://api.weixin.qq.com/cgi-bin/token?appid='+data.appid+'&secret='+data.secret+'&grant_type='+data.grant_type;if(accessTokenJson.access_token===""||accessTokenJson.expires_time{console.log("writeOK")});}});}}functiongetCollectionFeedback(res,req){//查询反馈constdist=req.body.tablename;//application/json返回的req.body是一个对象constquery="db.collection(\""+dist+"\").where({}).get()";constquerydata={env:data.env,query:query}consturl='https://api.weixin.qq.com/tcb/databasequery?access_token='+accessTokenJson.access_token;request({url:url,//请求路径method:"POST",//请求方式,默认为getheaders:{//设置请求头"content-type":"application/json",},body:JSON.stringify(querydata)//post参数string},function(error,response,body){if(!error&&response.statusCode===200){//编码类型res.setHeader('Content-Type','text/plain;charset=UTF-8');//返回代理内容//console.log("返回数据:"+JSON.stringify(body));res.end(body);}});}varserver=app.listen(apiPort,function(){console.log('代理接口,运行在http://'+hostIp+':'+apiPort+'/');})expresspost通过req.body获取客户端XMLHttpRequest发送的值,因为我们规定传输格式是application/json,所以这里的body是一个对象,可以直接使用类似req.body.tablename的方法来获取要查询的表名微信小程序数据库查询接口返回json数据格式:微信小程序数据库访问请参考云开发HTTPAPI文档