0.5.2 • Published 2 years ago
tsfinch v0.5.2
Finch
一个简单的TypeScript服务框架。
简介
用 TypeScript 写的;
跟其他的中间件系统差不多;
对原始的 Node 本身的 request
( IncomingMessage
) 和 response
( ServerResponse
) 没有做任何封装;用起来比较方便。
安装
npm install tsfinch
快速运行
import {Finch,log} from "tsfinch";
let app = new Finch();// app 是服务的名字可以随便起
app.listen(3000);
尽管这个服务总是返回404错误;但他已经运行起来了。
使用案例
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
HTTPS 服务;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)
JSONresponseWare.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
请求
0.4.4
2 years ago
0.5.0
2 years ago
0.5.2
2 years ago
0.5.1
2 years ago
0.4.3
2 years ago
0.4.2
2 years ago
0.4.1
2 years ago
0.4.0
2 years ago
0.3.0
3 years ago
0.2.2
3 years ago
0.2.1
4 years ago
0.2.0
4 years ago
0.1.8
4 years ago
0.1.7
4 years ago
0.1.6
4 years ago
0.1.5
4 years ago
0.1.2
4 years ago
0.1.3
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago