动态小程序需要后台服务器为前台提供数据。现一般数据格式为json,因为json相较于xml更加简洁,xml多了很多没必要的标记。后台服务器的语言有nodejs、php、java、c#、python等,koa是基于nodejs的web框架,本文介绍使用koa建设小程序服务器。
koalerplate是一个koa简洁的开始模板,操作步骤请参考https://github.com/dbalas/koalerplate中的Getting Started,在windows中修改文件名的指令为ren。将index.js中端口改为80。在cmd中curl localhost/v1/users?user=zhangsan请求,得到返回的json对象{"user":"zhangsan"}。
在小程序前端请求方式为:
wx.request({
url: 'localhost/v1/users?user=zhangsan',
success: function(res) {
console.log(res.data)
}
})