Skip to content

Commit 83d6566

Browse files
authored
Add quick start and some examples (Tencent#381)
docs(readme): add quick start and some examples
1 parent 412aabd commit 83d6566

12 files changed

+937
-1
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples

README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,66 @@ TSW 2.0 是在 1.0 的基础上抽丝剥茧,辅以现代化的设计模式演
2626

2727
<h2 align="center">Quick Start</h2>
2828

29-
TODO
29+
首先,通过 npm 或者 yarn 安装 npm 包,`npm install --save @tswjs/tsw` or `yarn add @tswjs/tsw`
30+
31+
原本通过 `node ./index.js` 方式启动的应用,更换为 `npx tsw ./index.js`
32+
33+
### Examples
34+
35+
我们提供了一些示例项目以让大家尽快了解该项目。
36+
37+
1. `cd ~`
38+
2. `git clone https://github.com/Tencent/TSW.git`
39+
3. `cd TSW`
40+
41+
#### Native `http.createServer`
42+
43+
1. `cd examples/http-create-server`
44+
2. `npx tsw ./index.js`
45+
3. `curl -v localhost:4443/path/to/foo -X POST -d "hello, server"`
46+
47+
#### Koa
48+
49+
1. `cd examples/koa`
50+
2. `yarn serve` 或者 `npm run serve`
51+
3. `curl -v localhost:4443/path/to/foo -X POST -d "hello, server"`
52+
53+
#### 使用 https://tswjs.org 开放平台
54+
55+
在默认的情况下,TSW 只是会把所有的日志和抓包内容抓取到并且送到事件总线上,以供 [插件](#插件是什么?) 消费。所以将日志和抓包内容落地查看一般需要用户自己编写插件以及提供存储,使用成本过于高昂。因此,TSW 官方提供了公共的服务平台,以供用户以更成本、更快、更方便地使用 TSW 的特性。使用方式如下:
56+
57+
1. 登录 https://tswjs.org 并在其上新建一个应用
58+
59+
![create-app](./static/images/create-app.png)
60+
61+
1. 打开应用,获取 `appid``appkey`
62+
63+
![appid-appkey](./static/images/appid-appkey.png)
64+
65+
1. 在项目根目录下新增配置文件 `tswconfig.json`,并将 `appid``appkey` 配置完成
66+
67+
```json
68+
{
69+
"appid": "your-app-id",
70+
"appkey": "your-app-key",
71+
"plugins": [
72+
"@tswjs/open-platform-plugin"
73+
]
74+
}
75+
```
76+
77+
1. 向之前启动的 Koa 或者原生 http server 发送请求,并且在开放平台上查看对应的日志和抓包。查看地址为下方地址拼接而成 `https://domain/log/view/demo`
78+
79+
![log-view](./static/images/log-view.png)
80+
81+
**日志记录**
82+
83+
![log](./static/images/log.png)
84+
85+
**在线查看抓包内容**
86+
87+
![capture](./static/images/capture.png)
88+
3089

3190
<h2 align="center">Plugins</h2>
3291

examples/http-create-server/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const http = require("http");
2+
3+
console.log("try to start a server");
4+
5+
http.createServer((req, res) => {
6+
res.status = 200;
7+
console.log(req.url)
8+
}).listen(4443);

examples/koa/koa.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Koa = require("koa");
2+
const axios = require("axios");
3+
4+
const app = new Koa();
5+
6+
app.use(async ctx => {
7+
await axios.get(
8+
"http://jsonplaceholder.typicode.com/todos/1"
9+
).then(res => {
10+
console.log(res.data);
11+
});
12+
13+
await axios.post("http://jsonplaceholder.typicode.com/posts", {
14+
body: JSON.stringify({
15+
title: 'foo',
16+
body: 'bar',
17+
userId: 1
18+
}),
19+
headers: {
20+
"Content-type": "application/json; charset=UTF-8"
21+
}
22+
}).then(res => {
23+
console.log(res.data);
24+
});
25+
26+
27+
ctx.body = "Hello, tsw 2.0";
28+
ctx.status = 200;
29+
}).listen(4443);

examples/koa/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "koa-example",
3+
"scripts": {
4+
"serve": "NODE_OPTIONS='--inspect=localhost:4442' tsw ./koa.js"
5+
},
6+
"version": "1.0.0",
7+
"main": "index.js",
8+
"license": "MIT",
9+
"dependencies": {
10+
"@tswjs/open-platform-plugin": "^0.0.2",
11+
"@tswjs/tsw": "^2.0.0-alpha",
12+
"axios": "^0.19.0",
13+
"koa": "^2.11.0"
14+
}
15+
}

examples/koa/tswconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"appid": "tsw1431",
3+
"appkey": "PwPaD4RRAsrSdRZjQSc3fbKM",
4+
"plugins": [
5+
"@tswjs/open-platform-plugin"
6+
]
7+
}

0 commit comments

Comments
 (0)