0.0.3 • Published 4 years ago
auto2http v0.0.3
auto2http
webStorm根据配置来自动生产 xx.http文件 用于对接口的测试 只能在node项目中使用 WebStorm automatically produces the xx.http file according to the configuration. It is used to test the API. It can only be used in the nodejs project.
const Auto2http = require("auto2http")
const path = require('path')
const route = Auto2http.createApiDict(path.join(__dirname, '../api'), true)
/* path.join(__dirname, '../api ')
接口文件的根目录 为绝对路径
The root directory of the interface file is an absolute path
*/
// true 是否自动生成提示 routes属性的提示
// Whether to automatically generate a prompt for the routes attribute
仅仅是因为要生成导出是有代码提示
main.configApiList([{}])
// Just because there is a code hint to generate the export
main函数
给app加路由配置 app.method(path,method)
Auto2http.main({ httpRoot: path.join(__dirname, "/dd"), // 成 http文件 的绝对路径 The absolute path of the http file
port: 8080, // 端口
hostname: "localhost", // 主机名
defaultHeader: {}, // 设置头 默认"Content-Type": "application/json","Accept": "application/json"
flag: "a+", // w 或 a+ 文件写入的mode 默认a+ 传 false或undefined 就不会去 写入 http文件
apiList: [{ // 路由 list
method: "post", // 请求方式
path: "/msa", // 路径
dynamic : {// 动态路由 Dynamic routing
path:"/:name",
test:"/lisa"
},
headers: { // 头
"Content-Type": "application/json"
},
params: { // 查询字符串
name: "丽萨"
},
data: { // data
},
description: "测试",
handler: route["/msa"], // 默认会调用对应的文件中的[this.method] This will require glad of [this.method]
port: 8081, // 独立的端口
hostname: "localhost", // 独立的主机名
plugins:{} // 独立的插件 Standalone plugin
}],
}).createApi({ // 完成 app.method(path,()=>{})的操作 express()
app: express(),
plugins: { // 每个文件导入公共的插件
// 在接口文件中调用
},
callback(app) { // 在配置路由前对app进行配置 Configure the app before configuring routing
app.use(express.json)
},
}).listen(8087)
路由编写
// 接口文件
// api/ msa.js
module.exports = (plugin) => {
return {
post: (req, res) => {
res.json({
msa: 'post'
})
},
get: (req, res) => {
res.json({
msa: 'get'
})
},
ws: (req, res) => {
}
}
}