代码拉取完成,页面将自动刷新
示例程序
var express=require('express');
var app =express();
var mysql = require('mysql');
var bodyParser = require("body-parser");
let https = require("https");
let fs = require("fs");
// Configuare https
const httpsOption = {
key : fs.readFileSync("./https/5394471_blogapi.zhangqx.com.key"),
cert: fs.readFileSync("./https/5394471_blogapi.zhangqx.com.pem")
}
app.use(bodyParser.json({limit: '1mb'})); //body-parser 解析json格式数据
app.use(bodyParser.urlencoded({ //此项必须在 bodyParser.json 下面,为参数编码
extended: true
}));
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
//Post 接口测试
app.post('/abc', function (req, res) {
var user_name=req.body.username;
console.log(req.body);
let result ={
"errcode":0,
"msg":"OK",
"data":'Got a POST request: username='+user_name
}
res.json(result);
})
//配置服务端口
var server = app.listen(80, function () {
var port = server.address().port;
console.log('App listening at %s', port);
})
https.createServer(httpsOption, app).listen(443);
前端请求数据
wx.request({
url: 'https://blogapi.zhangqx.com/abc',
method:'POST',
data: {
'username': 'user01'
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。