tsw.jstsw.js

快速开始

hello world

一. 环境要求

  • 操作系统: Windows/Mac/Linux
  • Node.js: 12.0.0+

二. 开始

1. 安装

npm install --save @tswjs/tsw
// yarn add @tswjs/tsw

2. 添加配置文件

配置文件是 TSW 启动时加载进运行时的配置文件,主要声明需要使用的 插件 列表。默认会加载项目根目录下的 tswconfig.js 文件,也可以通过启动参数 -c 或者 --config 来手动指定配置文件路径。
注意事项:2.0 中没有集成开放平台相关逻辑,而是封装成了一个插件让用户按需使用,详情见插件章节。

配置文件示例:

module.exports = {
  plugins: [
    new MyPlugin({})
  ]
}

参数列表:

Name

Type

Default

Optional

Description

plugins ...[Plugin] - yes 插件列表
cleanLog boolean false yes 是否关闭默认打印
winstonTransports ...[winston.transports.File] - yes winston 日志通道

3. 启动

npx @tswjs/tsw ./index.js

注意事项:原先 node --inspect ./index.js 中的 CLI 参数如 --inspect 需要转化为环境变量 NODE_OPTIONS 来执行,如 NODE_OPTIONS="--inspect" npx @tswjs/tsw ./index.js

使用ts

在保证项目有 ts-node 依赖包的情况下,按照如下方式执行即可直接加载 ts 文件。

NODE_OPTIONS="--require=ts-node/register" npx @tswjs/tsw ./index.ts

三. 示例代码

我们提供了一些示例项目以让大家尽快了解该项目。

  1. cd ~
  2. git clone https://github.com/Tencent/TSW.git
  3. cd TSW

Koa

  1. cd examples/koa
  2. yarn
  3. yarn serve 或者 npm run serve
  4. curl -v localhost:4443/path/to/foo -X POST -d "hello, server"

四. CLI (Command Line Interface)

使用 npx @tswjs/tsw --help 来获取 CLI 选项。