微信小程序的后端
微信小程序提供了一个后端云函数, 曾经尝试过, 这玩意就和微信开发工具一样出类拔萃, 就像黑暗中的萤火虫, 让人难忘. 因此, 这次再做后端, 死活都要自己弄一个.
参考
- 阮一峰从不让人失望: https://www.ruanyifeng.com/blog/2020/11/wechat-miniprogram-tutorial-part-four.html
挡板
- 使用json-server: https://www.npmjs.com/package/json-server
前端的调用
<input type="tel" name="" id="" placeholder="微信手机" model:value="" bindinput="inputmobile"></input>
<view class="fuck"></view>
<text class="okbutton" bind:tap="mobilelogin">登录</text>
<text class="" selectable="false" space="false" decode="false">
</text>
// get方式------------
mobilelogin(){
const that =this
//get方法
wx.request({//介破玩意不支持promise
url: 'http://localhost:8080/getest',
success(res) {
that.setData({ items: res.data });
}
});
}
// post方式---------建议都用json------------
wx.request({
//介破玩意不支持promise
url: 'http://localhost:8080/api',
//必须使用post
method: 'POST',
//必须是json对象
data: { xxx: '这是一个post' },
success (res) {
//res.data必然是个json对象
that.setData({ items: JSON.stringify(res.data) })
}
})
后端样例代码
import JSON5 from "json5"
import http from "http"
import fs from 'fs'
log = console.log
router = (dealio) ->
http.createServer (req, res) ->
#post要用的处理函数
out = (t) ->
res.writeHead 200,
"Access-Control-Allow-Origin": "*"
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS"
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Content-Length, Authorization, Accept"
res.end(JSON.stringify(t))
# 处理错误的函数, get方法不大会出错, 因为默认就去首页了, post方法可能有各种错误
dealerrors = (e) ->
log e
res.writeHead 500,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Content-Length, Authorization, Accept",
res.end JSON.stringify
stack: e.stack
target: e.target
e:e
# get方法要用的处理函数
serve=(path, type) ->
res.writeHead 200,
"Content-Type": type,
"Access-Control-Allow-Origin": "*"
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS"
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Content-Length, Authorization, Accept"
fs.createReadStream path
.on "error", (err)->
.pipe res
###
* 处理/index.html
* /js/xxx.js
-------------------------处理get---------------------
###
if req.method == "GET"
# log("ggggggggggggggggggget")
dirname = process.cwd()
# 处理js
if req.url.substr(-3) == ".js"
serve dirname + req.url, "application/javascript"
# log("js:", req.url)
return
# 处理图片
if req.url.substr(-4) == ".png"
serve dirname + req.url, "image/png"
# log("js:", req.url)
return
# 处理图片
if req.url.substr(-4) == ".ico"
serve dirname + req.url, "image/vnd.microsoft.icon"
# log("js:", req.url)
return
# 处理css
if (req.url.substr(-4) == ".css")
serve dirname + req.url, "text/css"
# log("js:", req.url)
return
# 处理网页
if (req.url.substr(-5) == ".html")
serve dirname + req.url, "text/html"
# log("js:", req.url)
return
# 处理getest
if (req.url.substr(-6) == "getest")
out "完全正常"
# log("js:", req.url)
return
# 处理index.html
serve "#{dirname}/index.html", "text/html"
# log('default:', req.url)
return
###
* --------------------处理option, 为了跨域---------------------
###
if req.method == "OPTIONS"
res.writeHead 200,
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Content-Length, Authorization, Accept",
res.end()
return
###
* -------------------处理post接口调用----------------------------
###
if req.method == "POST"
body = "" # 这个不能拿到外边去, 这个不能作为全局变量处理
# 处理网页
return unless (req.url.substr(-3) == "api")
req.on "data", (c) ->
body += c
# log('bodyon',body)
req.on "end", () ->
try
console.log "mrouter-index-body:", body
out(dealio(JSON5.parse(body)))
catch e
dealerrors e
# pass exception object to error handler -> your own function
.listen 8080
export default router