当前位置:   article > 正文

nodejs 开发api 环境的简单搭建_npm 开发环境api接口

npm 开发环境api接口

nodejs 开发api 环境的简单搭建

1基础环境
1.1 nodejs npm express
1.2 安装 npm install express-generator -g
2 创建项目
2.1express --view=pug myapp

在这里插入图片描述

2.2 安装依赖
cd myapp && npm install
  • 1
2.3 安装dev启动模块
npm i supervisor -g
  • 1
2.4 修改dev启动项

在这里插入图片描述

2.5 启动dev 项目
npm run dev
  • 1

在这里插入图片描述

3 创建接口文件
3.1创建目录及文件

在这里插入图片描述

3.2 引入接口及注册接口在这里插入图片描述

4.编写接口文档

4.1安装依赖
npm i apidoc -g
  • 1
4.2修改package.json 配置

在这里插入图片描述

4.3修改user.js 接口内容
 /**
 * @api {get} /user/:id Request User information
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
4.4 开启新的命令窗口
apidoc -i api/ -o public/apidoc/
  • 1

在这里插入图片描述

4.5 浏览器打开
http://localhost:5000
  • 1

在这里插入图片描述

4.6 修改端口号的地方

在这里插入图片描述

环境基本完成

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/723501
推荐阅读
相关标签
  

闽ICP备14008679号