# tsfinch

> A simple typescript HTTP server.

Latest version **0.5.2** (published 2022-08-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install tsfinch
pnpm add tsfinch
yarn add tsfinch
bun add tsfinch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.2 |
| Published | 2022-08-25 |
| First published | 2020-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 59.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | BenjaminHsu |
| Maintainers | benjaminhsu0072 |

## Links

- npm: https://www.npmjs.com/package/tsfinch
- Repository: https://gitee.com/benjaminhsu/finch
- npm.io page: https://npm.io/package/tsfinch

## Dependencies (3)

- [busboy](https://npm.io/package/busboy.md) ^0.3.1
- [cookie](https://npm.io/package/cookie.md) ^0.5.0
- [finch-log](https://npm.io/package/finch-log.md) ^0.2.5

## Recent versions

- 0.5.2 (latest) — 2022-08-25
- 0.5.1 — 2022-08-25
- 0.5.0 — 2022-08-25
- 0.4.4 — 2022-08-23
- 0.4.3 — 2022-05-17
- 0.4.2 — 2022-05-16
- 0.4.1 — 2022-04-20
- 0.4.0 — 2022-03-28
- 0.3.0 — 2021-10-28
- 0.2.2 — 2021-04-22
- 0.2.1 — 2020-05-16
- 0.2.0 — 2020-05-15
- 0.1.8 — 2020-05-11
- 0.1.7 — 2020-04-27
- 0.1.6 — 2020-04-27
- … 5 more at https://npm.io/package/tsfinch/versions

## README

# Finch

一个简单的TypeScript服务框架。

## 简介

用 TypeScript 写的；  
跟其他的中间件系统差不多；  
对原始的 Node 本身的 `request`( `IncomingMessage` ) 和 `response` ( `ServerResponse`) 没有做任何封装；用起来比较方便。

## 安装

` npm install tsfinch`

## 快速运行

```typescript
import {Finch,log} from "tsfinch";

let app = new Finch();// app 是服务的名字可以随便起
app.listen(3000);
```

尽管这个服务总是返回404错误；但他已经运行起来了。

## 使用案例

```typescript
import {Finch, postParser, responseWare} from "tsfinch";

//初始化一个APP
let app = new Finch();

//设置一个静态文件的路径，应用会在此处查找静态文件并向请求者自动返回
app.staticDir = "./files";

//一个中间件的使用示例：当用户访问域名根目录时，直接返回"HELLO WORLD!"
app.use('/', (req, res, next) => { //中间件函数
  res.writeHead(200, {'content-type': 'text/plain'});// 'res' 对象就是原生的 'ServerResponse' 对象
  res.write('HELLO WORLD!');
  res.end();
});

//使用自带的 responseWare 可以简化操作
app.use('/responseJson', (req, res, next) => {
  responseWare.rJson(res, {"Hello": "World"});
});

//多个中间件套用
app.use('/upload', postParser);
app.use('/upload', (req, res) => {

  let fieldStr = JSON.stringify(req.postFields);
  let fileStr = JSON.stringify(req.postFiles);

  //使用内置的日志组件打印上一步被中间件解析的文件信息；
  log(fieldStr+"\r\n"+fileStr);

  res.writeHead(200, {'content-type': 'text/plain'});
  res.write('received upload:\n\n');
  res.end();
});

//开始监听端口进行服务
app.listen(3000);
```

## Finch类
提供HTTP服务的主类

### 属性
* `staticDir` 静态文件路径
* `logger` FinchLog 日志实例，详见下文；
* `enableLogger` 是否开启 FinchLog 日志实例；
* `handles` 用以处理请求路径所对应的中间件集合；
* `server` HTTP[S] 服务；Node原生；

### 方法
* `listen(port: number)` 监听端口
* `use(handle: string, middleWare: middleWareFunction): void` 使用中间件
* `all(middleWare: middleWareFunction):void` 对所有句柄使用中间件

### FinchLog类
内置用来打印日志的类

#### 属性
* `logFileMaxLines` 日志文件的最大行数，限制日志文件的大小
* `maxLogFilesCount` 最多储存多少个日志文件
* `logStorageDir` 日志文件的路径
#### 方法
* `log(message: string, color?: FinchLogColor)` 打印日志的文件

## responseWare 方法组
* `responseWare.rJson(response: ServerResponse, msg: string | object)` JSON
* `responseWare.r404(response:ServerResponse) `
* `responseWare.r403(response:ServerResponse)`
* `responseWare.rStaticFiles(res:ServerResponse, staticFilePath:string)` 返静态文件
* `responseWare.rDownloadFile(res:ServerResponse, dfp:string, dfn?:string)` 返下载文件
* `responseWare.rRedirect(response: ServerResponse, url: string)` 返回一个HTML页面让他重新跳转

## cookieParser 中间件

使用“cookie”解析cookie

## postParser 中间件
使用 “BusBoy” 解析`POST`请求

---
_Source: https://npm.io/package/tsfinch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
