koa-api-gen
v0.1.9
Published
快速生成基于 Koa 框架,用于开发 Web 服务器 API 项目 CLI,包含了一些常见的内置功能,可以根据选择不同的配置来生成你需要的模板
Downloads
10
Readme
KOA API GEN
快速生成基于 Koa 框架,用于开发 web 服务器 API 项目 CLI,包含了一些常见的内置功能,可以根据选择不同的配置来生成你需要的模板
Tip:此工程目前略显简略,并没有太多的 ec 判定,所以为了保证程序的执行,使用请尽量与文档保持一致
安装
# 局部安装
npm install koa-api-gen
# 全局安装-推荐
npm install -g koa-api-gen
使用
安装后可以通过 koa-api-gen
来创建,也可以使用简写kag
创建一个项目:
kag create my_project
# 如果是局部安装,你需要添加 npx 执行
npx kag create my_project
my_project 可以替换为你自定义的名称,命名尽量使用英文
回车执行,你会遇到一些提问,根据这些提问来对你要创建的项目进行一些配置:
- 端口号: 项目创建完成,启动时监听的端口号,如:
http://localhost:5050
- 风格:
- EASY: 简单的配置,通常只有一个入口文件,适合平常做一些测试时,需要快速启动一个服务器配合
- STANDARD: 标准的配置,内置的模板是我个人常用的项目模板,适合需要开发一个常规的的web服务器
- 配置: 包含了一些内置的配置
- KoaRouter: 使用 koa-router 实现路由处理,当选择 STANDARD 风格时,此项默认必选,详情查阅
- KoaStatic: 开启静态文件访问,详情查阅
- KoaCors: 解决 CORS,详情查阅
- KoaBody: 解析请求的参数,如 get 请求将会将参数保存在 ctx.request.query,而 post 等请求体则会保存在 ctx.request.body 上,详情查阅
- GlobalError: STANDARD 风格专属,全局异常捕获的方案,通过异常的方式对前端的请求做出一系列的响应,并捕获未知错误,保证程序的正常运行,详情查阅
- Database: STANDARD 风格专属,选择此项,后续将会依次填写数据库的名称、主机、端口、用户名、密码
经过上述的步骤之后,将会创建模板并安装依赖,成功之后如图所示:
注意事项
当需要创建的项目已经存在
默认会询问,是否进行覆盖,你也可以通过添加参数进行强制覆盖,如:
kag create my_project -f
地址无法访问
如果出现地址无法访问,可能尝试换一个端口号再次尝试一下,如图: