当前位置: 首页 > news >正文

手机信息分类网站制作网站制作模版

手机信息分类网站制作,网站制作模版,搜索引擎优化 简历,佛山专业的做网站的文章目录 结构demo步骤demo运行效果API测试(1) 添加待办事项(2) 获取所有待办事项(3) 切换完成状态(4) 删除待办事项 API测试-RESTClient一些其他的高级功能环境变量管理不同环境配置授权认证 测试需要登录的接口保存响应测试脚本编写自动化测试 bug解决 结构 尝试写一个简单的… 文章目录 结构demo步骤demo运行效果API测试(1) 添加待办事项(2) 获取所有待办事项(3) 切换完成状态(4) 删除待办事项 API测试-RESTClient一些其他的高级功能环境变量管理不同环境配置授权认证 测试需要登录的接口保存响应测试脚本编写自动化测试 bug解决 结构 尝试写一个简单的待办事项Todo管理的NodeJs后端服务文件架构如下 zyxTest/ ├── server.js # 主程序 ├── package.json # 项目配置 └── .gitignore # 忽略文件demo步骤 初始化项目并安装依赖 express框架似乎是nodejs写小程序的常用框架我们先用express进行尝试 npm init -y #需要首先在windows powershell里面运行 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser不然会弹出vscode禁止运行脚本 npm install express创建 server.js const express require(express); const app express(); app.use(express.json());// 模拟数据库内存存储 let todos []; let idCounter 1;// 获取所有待办事项 app.get(/todos, (req, res) {res.json(todos); });// 添加新待办事项 app.post(/todos, (req, res) {const { title } req.body;if (!title) {return res.status(400).json({ error: Title is required });}const newTodo { id: idCounter, title, completed: false };todos.push(newTodo);res.status(201).json(newTodo); });// 删除待办事项 app.delete(/todos/:id, (req, res) {const id parseInt(req.params.id);todos todos.filter(todo todo.id ! id);res.sendStatus(204); });// 切换完成状态 app.patch(/todos/:id/toggle, (req, res) {const id parseInt(req.params.id);const todo todos.find(t t.id id);if (todo) {todo.completed !todo.completed;res.json(todo);} else {res.status(404).json({ error: Todo not found });} });// 启动服务器 const PORT 3000; app.listen(PORT, () {console.log(Server running at http://localhost:${PORT}); });vscode终端启动服务器 node server.jsdemo运行效果 此时vscode终端会给出访问链接 点击链接可以看到前端状态此处采用了最简单的写法 API测试 我们最初采用curl进行api测试但win里面的curl不太好用详情见bug解决第三条改用vscode的RestClient插件进行api测试。 这个插件能帮助我们发送写好的http请求效果类似postman 插件效果如下红框内部是模拟请求发送按钮。 (1) 添加待办事项 curl方法 curl -X POST http://localhost:3000/todos \-H Content-Type: application/json \-d {title: Buy milk}使用插件编写test.http方法 POST http://localhost:3000/todos Content-Type: application/json{title: 使用 REST Client 测试 }获取到响应测试成功 (2) 获取所有待办事项 curl http://localhost:3000/todos### 获取待办事项 GET http://localhost:3000/todos响应如下测试成功 (3) 切换完成状态 curl -X PATCH http://localhost:3000/todos/1/togglePATCH http://localhost:3000/todos/1/toggle响应如下测试成功 (4) 删除待办事项 curl -X DELETE http://localhost:3000/todos/1### 删除待办事项 (DELETE) DELETE http://localhost:3000/todos/1也可以通过name add_todo使用 # name 请求名称 语法为请求命名后续引用响应可以切换单独某个请求的完成状态 ### 1. 添加新待办事项并命名请求 # name add_todo POST http://localhost:3000/todos Content-Type: application/json{title: 使用变量示例的任务 }### 2. 从响应中提取ID并赋值给变量 todoId {{add_todo.response.body.id}}### 3. 切换完成状态使用变量 PATCH http://localhost:3000/todos/{{todoId}}/toggle### 4. 删除待办事项使用同一个变量 DELETE http://localhost:3000/todos/{{todoId}}API测试-RESTClient一些其他的高级功能 环境变量管理不同环境配置 ### 设置变量 dev http://localhost:3000 prod https://api.yourserver.com### 使用变量 GET {{dev}}/todos授权认证 测试需要登录的接口 POST http://localhost:3000/login Content-Type: application/json{username: admin,password: 123456 }### 获取token后使用 token {{login.response.body.token}} GET http://localhost:3000/profile Authorization: Bearer {{token}}保存响应 GET http://localhost:3000/todosresponse.json测试脚本编写自动化测试 GET http://localhost:3000/todos {%client.test(Status OK, function() {client.assert(response.status 200);});client.test(Has items, function() {client.assert(response.body.length 0);}); %}bug解决 端口占用 # 查找占用3000端口的进程 netstat -ano | findstr :3000 #mac似乎是lsof -i :3000# 终止进程 taskkill /PID PID /F #mac是kill -9依赖安装失败 尝试清除缓存 npm cache clean --force rm -rf node_modules package-lock.json npm installwindows的curl问题 在 Windows PowerShell 中curl 命令实际上是 Invoke-WebRequest cmdlet 的别名所以我们在win下直接用curl会报错 win下可以直接使用 PowerShell 原生命令进行测试 Invoke-RestMethod -Uri http://localhost:3000/todos -Method POST -Headers {Content-Typeapplication/json} -Body {title:新任务}但是还是比较建议在 VSCode 中用 REST Client 扩展更加方便 创建 test.http 文件添加内容 ### 添加待办事项 POST http://localhost:3000/todos Content-Type: application/json{title: 使用 REST Client 测试 }### 获取待办事项 GET http://localhost:3000/todos再点击每个请求上方的 “Send Request”就是发送请求
http://www.tj-hxxt.cn/news/139447.html

相关文章:

  • 如何让百度快照找到自己的网站望野
  • 电子商务网站建设方一般做网站都在什么网做
  • 微信优惠券网站怎么做的区块链开发
  • 重庆佳宇建设集团网站有货 那样的网站怎么做
  • 学习软件开发的网站wordpress作品集
  • 河南网络建站网站建设模块怎么使用
  • 企网官方网站移动端优秀网站
  • 网站商城运营成本怎么网站建设怎么样
  • 广州网站建设是什么公司网站在哪里做
  • 买了个域名 如何建网站网络营销专业
  • 网站301重定向检测wordpress自定义函数
  • 网站如何快速推广如何提高网站收录
  • 公司网站开发可行性报告学app软件开发多少钱
  • 商业网站建设费用商丘网络科技有限公司
  • 网站备案怎么备案江苏省城乡建设网站
  • 长沙免费网站建站模板湖南省住房与城乡建设厅网站官网
  • 北京网站建设产品介绍做网站域名费一般多少钱
  • 网站建设流程哪家好什么都能买到的网站
  • 设计师的招聘要求seo综合查询
  • 温州做网站多少钱汽车设计公司排名前十强
  • 北京网络销售公司绍兴seo网站管理
  • 域名跟空间都有了怎么做网站网络品牌塑造
  • 耐克运动鞋网站建设规划书框架哪个网站可以领手工回家做
  • 自己做视频类网站用哪个cms中国互联网协会秘书长
  • 邢台市网站建设网站方案设计
  • 外贸网站设计多少钱买正品去哪个网站最好
  • 建设手机银行网站市场营销计划
  • 如何查找高权重网站个人简历模板免费可编辑
  • 聊城网站制作企业邮箱在哪看
  • wordpress商品多选互联网seo是什么