在build文件夹中找到 webpack-dev-conf.js配置文件,在
const portfinder = require("portfinder");
之后插入代码,导入express模块以及superagent(客户端请求代理)模块
const express = require("express");
const superagent = require("superagent"); // 客户端请求代理模块
导入模拟后台的数据以及设置路由变量
let appData = require("../mockdata.json"); // 加载本地数据
let self = appData.self;
let friend = appData.friends;
const app = express();
const apiRouter = express.Router();
app.use("/api", apiRouter);
在devServer属性中插入before属性
before(app) {
app.get("/api/self", (req, res) => {
res.json({
data: self
});
});
app.get("/api/friends", (req, res) => {
res.json({
data: friend
});
});
// 聊天机器人api
app.get("/api/robotapi", (req, res) => {
let response = res,
info = req.query.message,
userID = req.query.id,
key = "069e90c4262243bf964ad95014371384";
superagent
.post("http://www.tuling123.com/openapi/api")
.send({ info, userID, key })
.end((err, res) => {
if (err) {
console.log(err);
}
response.json({
data: res.text
});
});
});
}
配置完成之后一定要重新编译代码才会生效
来源:CSDN
作者:YUHUI01
链接:https://blog.csdn.net/YUHUI01/article/details/88649407