node-grid-cli
v1.0.2
Published
Grid' application generator.
Downloads
6
Readme
一个 nodejs 后端服务框架,具有自动生成文档,批量注册接口,自动记录日志等功能 ...
安装
npm i -g node-grid-cli
初始化
在当前目录下构建醒目
grid create <projectName>
在当前目录下初始化一个项目
文件夹必须为空
grid init
修改项目模板(自动打开模板目录)
以下命令效果相同
grid show
grid template
grid templates
grid change
使用
注册接口
统一在 ./src/routes 目录下的 index.js 中注册。
注册格式:
module.exports = {
<接口地址>: {
<方法>: <函数文件相对于 models 文件夹的路径>
<二级接口>: {
<方法>: <函数文件相对于 models 文件夹的路径>
}
}
}
配置文件
此配置文件不可删除,默认可以什么都不写
配置项解释:
module.exports = {
basic: {
// 多线程模式,默认关闭
thread: true,
// 设置 host 地址
// host: '127.0.0.1',
// 设置端口号,默认3000
// port: 3000,
// 解析 POST 请求的请求体,默认开启
// bodyParser: true,
// 解析请求携带的 cookie,默认开启
// cookieParser: true,
},
staticResource: {
// 是否启用静态资源托管,默认开启
// enable: true,
// 静态资源地址,默认为'/'
// path: '/',
// 静态资源所在目录,默认为'./public'
// dirPath: './public',
},
log: {
/**
* close: 关闭
* file: 写入文件
* console: 输出到控制台(默认)
*/
// mode: 'console',
// 日志输出目录,默认为'./log'
// filePath: './log',
// 文件名中的日期部分格式
// 默认值
// dateFormat: 'YYYY-MM-DD',
// 文件名格式
// 默认值
// fileName: 'Access-%DATE%.log',
},
// 响应头统一设置
response: {
// origin 为 * 时自动不接收 cookie
origin: '*',
credentials: false,
ContentType: 'application/json;charset=utf-8',
allow_methods: ['GET, POST, OPTIONS', 'PUT', 'DELETE'],
allow_headers: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
},
doc: {
// 开启自动生成文档,默认关闭
enable: true,
// 文档输出目录
// 默认值
// dirPath: './doc',
// 文档预览地址
// 默认值
// path: '/api-doc',
// 文档名
name: '自定义的文档标题',
// 仓库地址
// gitLine: 'https://gitee.com/ken200461/node-grid-cli',
// 主题颜色,默认为绿色
// themeColor: '#000',
// 文档文字计数器设置
// count: {
// countable: true,
// 语言,默认为英文
// language: 'chinese',
// },
// 代码复制按钮
// 默认值
// copyCode: {
// buttonText: 'copy',
// errorText: '复制失败',
// successText: '复制成功',
// },
},
}
关于日志
模式选择为 file 后,只会记录失败的请求。日志文件会按照天分开,避免文件过大。
生成文档
根据 models 下处理函数文件上的 JSDoc 注释生成,当 GET 和 POST 请求的处理函数都有注释时会优先解析 GET 请求处理函数上的注释
支持的 JSDoc 注释:
/**
* // 接口是否过期
* @deprecated
* @description 接口解释文字
* @group 接口分组
* @name 接口名称
* // 需要的参数
* // 在参数名后加 * 代表必填
* // 花括号中为参数类型
* @apiParam {string} param1* 参数1说明
* @apiParam {number} param2 参数2说明
* // 接口返回值
* @return {string} return1 返回值1说明
* @return {number} return2 返回值2说明
* // 请求示例
* // GET 请求可以直接写地址
* @example {url} /api/user/login?username=admin&password=123456
* // json格式,写在json上的注释(不要有空格) json内容
* @example {json} data {"key":"value","key2":"value2"}
* // object 类型的键也需要加上双引号!!!
* @example {object} object {"key":"value","key2":"value2"}
* // 其余数据类型会被拼接为 axios 请求的代码后展示
* @example {boolean} boolean true
* @example {number} num 18
* @example {string} str hello
*/