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

网站建设好弄不好弄网站定制价格

网站建设好弄不好弄,网站定制价格,网站空间ftp,网站开发不懂英语typed-rest-client 是一个用于 Node.js 的库,它提供了一种类型安全的方式来与 RESTful API 进行交互。其主要功能包括: 安装 typed-rest-client 要使用 typed-rest-client,首先需要安装它,可以通过 npm 来安装: $ n…

typed-rest-client 是一个用于 Node.js 的库,它提供了一种类型安全的方式来与 RESTful API 进行交互。其主要功能包括:

安装 typed-rest-client

要使用 typed-rest-client,首先需要安装它,可以通过 npm 来安装:

$ npm install typed-rest-client

使用 typed-rest-client

这里假定有个 express 的 server 提供了两个 REST API,一个是获取用户列表,一个是获取用户信息。

index.ts

import express, { Express, Request, Response } from "express";const app: Express = express();
const port = process.env.PORT || 3000;app.get("/", (req: Request, res: Response) => {res.send("Express + TypeScript Server");
});app.get("/users", (req: Request, res: Response) => {const users = [{name: 'kongxx',password: 'password',email: 'kongxx@example.com'},{name: 'Mandy',password: 'password',email: 'mandy@example.com'}]res.json(users);
});app.get("/users/:id", (req: Request, res: Response) => {const user = {name: 'kongxx',password: 'password',email: 'kongxx@example.com'}res.json(user);
});app.listen(port, () => {console.log(`[server]: Server is running at http://localhost:${port}`);
});

下面是测试程序

test.ts

import {RestClient, IRestResponse} from 'typed-rest-client/RestClient';interface User {name: string;password: string;email: string;
}async function test() {const rc: RestClient = new RestClient('test', 'http://localhost:3000');const resUsers: IRestResponse<User[]> = await rc.get<User[]>('/users');console.log('get users ...');console.log('response: ', resUsers);console.log('statusCode: ', resUsers.statusCode);console.log('name: ', resUsers.result[0]?.name);console.log('email: ', resUsers.result[0]?.email);const resUser: IRestResponse<User> = await rc.get<User>('/users/1');console.log('get user ...');console.log('response: ', resUser);console.log('statusCode: ', resUser.statusCode);console.log('name: ', resUser.result?.name);console.log('email: ', resUser.result?.email);
}test();
  • 这里首先定义了一个 interface,描述了 REST API 返回使用的数据结构。
  • 调用 RestClientget 方法,传入 URL 和返回的数据类型,返回一个 IRestResponse 对象,IRestResponse 对象包含了 HTTP 响应的状态码、响应头和响应体。
  • 通过 statusCode 属性可以获取到 HTTP 响应的状态码。
  • 通过 headers 属性可以获取到 HTTP 响应头。
  • 通过 result 属性可以获取到响应体中的数据。

测试

首先启动express server。

$ npm run dev

运行测试程序

$ npm install -g typescript
$ tsc src/test.ts  && node src/test.jsget users ...
response:  {statusCode: 200,result: [{name: 'kongxx',password: 'password',email: 'kongxx@example.com'},{name: 'Mandy',password: 'password',email: 'mandy@example.com'}],headers: {'x-powered-by': 'Express','content-type': 'application/json; charset=utf-8','content-length': '137',etag: 'W/"89-50ejbxheoPkdk58Nm75VjrVs3YE"',date: 'Mon, 23 Sep 2024 01:01:04 GMT',connection: 'close'}
}
statusCode:  200
name:  kongxx
email:  kongxx@example.comget user ...
response:  {statusCode: 200,result: { name: 'kongxx', password: 'password', email: 'kongxx@example.com' },headers: {'x-powered-by': 'Express','content-type': 'application/json; charset=utf-8','content-length': '68',etag: 'W/"44-WML8FV1wUhoW//8kQuCB8B/FWaQ"',date: 'Mon, 23 Sep 2024 01:01:04 GMT',connection: 'close'}
}
statusCode:  200
name:  kongxx
email:  kongxx@example.com
http://www.yayakq.cn/news/858719/

相关文章:

  • 做网站如何让用户注册五华网站建设 优帮云
  • 如何自创网站微信网页制作的软件
  • 电子商务系统网站设计弄几个的网站
  • 站长之家的seo综合查询工具外包公司被辞退有补偿吗
  • 外贸服装商城网站建设福州企业建设网站
  • 做业务查牙医诊所一般用什么网站大丰网站建设公司
  • 静态网站是什么哪里找网站建设的兼职
  • 企业网站建设课程体会wordpress 客户端
  • 临河 网站建设泉州专业网站开发公司
  • 申请域名之后如何做网站淮安网站seo
  • 免费微场景制作网站济南网站优化推广
  • 怎样做编辑发到网站成都app程序开发
  • 服装店网站模板单页面网站怎么做seo
  • 网站自己怎么建设网站关键词怎么优化
  • 西安买公司的网站建设重庆锅炉网站建设公司
  • 上传网站模板建站新冠咳嗽一般要咳多少天
  • 虚拟机做的网站怎么让外网访问不了python开发微信小程序教程
  • 做网站开发工资怎样做本地网站需要的软件
  • 可以做旅行行程的网站修改wordpress 的权限
  • 域名连接网站wordpress儿童主题
  • 万网网站建设购买过程网站一直做竞价么
  • 设计用哪些网站有哪些功能成品在线视频免费入口
  • 个人博客网站取名亚马逊平台的运营模式
  • 宝安做网站公司WordPress多站点恢复
  • 建立网站就是制作网页对吗厦门市保障性住房官网
  • 建设网站进行商品营销的重要性jsp网站开发的使用表格
  • 新手做网页用什么软件广州seo公司哪个比较好
  • 我想建立一个网站wordpress清理
  • 郑州的网站建设公司哪家好如何把网站提交到百度
  • 汽车网站设计dede网站地图样式修改