Intro to HTTP/2

简介
HTTP/2 主要目的是提高网页性能,最近几年比较火,将其单独抽成一 块讲。2015 年,HTTP/2 发布。它不叫 HTTP/2.0,是因为标准委员会不打算再发布子版本了,下一个新版本将是 HTTP/3。
目前还有不少服务还是 HTTP/1.1,NodeJS 也是从 v10 才将 http2 转正。Express5.x 才开始支持 http/2。

可以打开谷歌首页看看,基本上都是 http/2 协议,简写成 h2
HTTP/2 主要目的是提高网页性能,最近几年比较火,将其单独抽成一 块讲。2015 年,HTTP/2 发布。它不叫 HTTP/2.0,是因为标准委员会不打算再发布子版本了,下一个新版本将是 HTTP/3。
目前还有不少服务还是 HTTP/1.1,NodeJS 也是从 v10 才将 http2 转正。Express5.x 才开始支持 http/2。
可以打开谷歌首页看看,基本上都是 http/2 协议,简写成 h2
TS 允许将一些不同类型的变量相互赋值,虽然某种程度上可能会产生一些不可靠的行为,但增加了语言的灵活性,毕竟它的祖宗 JS 就是靠灵活性起家的,而灵活性已经深入前端的骨髓,有时会不得不做一些妥协。
当一个类型 Y 可以被赋值给另一个类型 X 时,我们就可以说类型 X 兼容类型 Y。
X (目标类型) = Y (源类型)
原始类型的兼容性主要关注三点:
npm | yarn | 功能描述 |
---|---|---|
npm run | yarn run | 运行 package.json 中预定义的脚本 |
npm config list | yarn config list | 查看配置信息 |
npm config set registry 仓库地址 | yarn config set registry 仓库地址 | 更换仓库地址 |
npm init | yarn init | 互动式创建/更新 package.json 文件 |
npm list | yarn list | 查看当前目录下已安装的所有依赖 |
npm login | yarn login | 登录你的用户名、邮箱 |
npm logout | yarn logout | 退出你的用户名、邮箱 |
npm publish | yarn publish | 将包发布到 npm |
npm test | yarn test(yarn run test) | 测试 |
npm bin | yarn bin | 显示 bin 文件所在的安装目录 |
yarn info | yarn info | 显示一个包的信息 |
curl -o- -L https://yarnpkg.com/install.sh | bash
如需升级,再次运行此命令,然后会出现以下信息:
/Users/xxx/.yarn already exists, possibly from a past Yarn install.
> Remove it (rm -rf /Users/xxx/.yarn) and run this script again.
根据提示,由于已经安装了 yarn,所以需要先删除~/.yarn
文件,然后再重新执行该命令,即可安装最新版 yarn。
泛型即不预先确定的数据类型,具体的类型在使用的时候才能确定。本节将学习ts
中的泛型函数。
在软件工程中,开发定义良好且可重用的组件是非常重要的,因为这确保了程序的灵活性和长期可扩展性。使用泛型可以创建可重用的组件,一个组件可以支持多种类型的数据,而用户就可以根据自己的需要的数据类型来使用组件。
概述中泛型的概念比较抽象,为了理解什么是泛型函数,我们可以结合一个例子来说明。
如下例所示,identity
函数返回传入的参数,但是这个函数的可重用性很差,只能支持number
类型。
function identity(arg: number): number {
return arg;
}
为了支持更多的类型,我们可以将参数设置为联合类型或者any
类型 。
Interfaces can extend one or more interfaces. This makes writing interfaces flexible and reusable.
interface Person {
name: string;
gender: string;
}
interface Employee extends Person {
department: string;
}
let empObj: Employee = {
name: 'Kimi',
gender: 'male',
department: 'Payment',
};
Interface in TypeScript can be used to define a type and also to implement it in the class. The most scenarios can be summarized as following:
In this section, we mainly focus on the object and array type definition using interface. As function type definition will be introduced in Function: Types section.
Interface likes contract which defines the structure of the object, array, function and also class. However, we need to note that the TypeScript compiler does not convert interface to JavaScript. It uses interface for type checking.
在TypeScript
项目中,tsconfig.json
文件指定了用来编译这个项目的根文件和编译选项,通过自定义tsconfig.json
文件中的配置项,可以达到我们想要的编译结果。
tsc
当我们使用tsc
命令对项目进行编译时,编译器会从当前目录开始去查找tsconfig.json
文件,逐级向上搜索父目录。
下面我们将通过以下三个方面来讲述tsconfig.json
配置:
files
、include
、exclude
compilerOptions
extends
、references
files
指定一个包含相对或绝对文件路径的列表,列举在files
中的所有文件,编译器在编译时都会将它们包含在内。
// tsconfig.json
"files": [
"src/core.ts",
"src/index.ts",
]
当配置文件中的files
字段值如上所示时,使用tsc
命令编译时,src
目录下的core.ts
和index.ts
文件会被编译为core.js
和index.js
文件。
目前 HTTP 在 Web 开发中被广泛使用,在 Web 开发中不同的 HTTP 响应状态码往往有不同的含义。在普通网站开发、分布式集群开发、团队协作方面,如果采用了 HTTP 作为组件之间交互的协议,遵守通用的响应状态码是很有必要的(一是有充分的信息量、二是避免歧义)。