qyani-web 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -8
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,14 +1,99 @@
|
|
1
|
+
# 镜像地址
|
2
|
+
[国内镜像](https://registry.npmmirror.com/)
|
3
|
+
```shell
|
4
|
+
npm config set registry https://registry.npmmirror.com
|
5
|
+
```
|
1
6
|
|
2
|
-
|
7
|
+
# 快速开始
|
8
|
+
**1.安装NodeJs环境**
|
9
|
+
[NodeJs下载](https://nodejs.org/en/download/)
|
10
|
+
**2.创建项目**
|
3
11
|
```shell
|
4
|
-
|
12
|
+
mkdir my-project
|
13
|
+
cd my-project
|
14
|
+
npm init -y
|
15
|
+
npm install qyani-web
|
5
16
|
```
|
6
|
-
|
7
|
-
1. 安装TypeScript
|
17
|
+
**3.启动demo**
|
8
18
|
```shell
|
9
|
-
|
19
|
+
node node_modules/qyani-web/demo/index.js
|
20
|
+
```
|
21
|
+
|
22
|
+
# 项目介绍
|
23
|
+
qyani-web是一个基于NodeJs的web开发框架,具有**可拓展**,**模块化**,**简单易用**的特点
|
24
|
+
|
25
|
+
## 核心类
|
26
|
+
```typescript
|
27
|
+
/**
|
28
|
+
* 核心类
|
29
|
+
* @class WebApp
|
30
|
+
*/
|
31
|
+
const app=new WebApp();
|
32
|
+
|
33
|
+
const groupRoute=new GroupRoute();
|
10
34
|
```
|
11
|
-
|
35
|
+
## 编写中间件
|
36
|
+
|
37
|
+
```typescript
|
38
|
+
import http from "http";
|
39
|
+
|
40
|
+
/**
|
41
|
+
* 中间件
|
42
|
+
* @interface MiddleWare
|
43
|
+
* @param {http.IncomingMessage} req
|
44
|
+
* @param {http.ServerResponse} res
|
45
|
+
* @returns {boolean} 返回true则继续执行下一个中间件,返回true则结束请求
|
46
|
+
*/
|
47
|
+
type MiddleWare = (req: http.IncomingMessage, res: http.ServerResponse)=>boolean|Promise<boolean>;
|
48
|
+
```
|
49
|
+
|
50
|
+
## 注册中间件
|
51
|
+
```typescript
|
52
|
+
app.beforeRequest(middelware)
|
53
|
+
groupRoute.beforeRequest(middelware)
|
54
|
+
app.afterRequest(middelware)
|
55
|
+
groupRoute.afterRequest(middelware)
|
56
|
+
```
|
57
|
+
## 注册路由
|
58
|
+
```typescript
|
59
|
+
type handler = (req: http.IncomingMessage, res: http.ServerResponse) => void|boolean|Promise<void|boolean>;
|
60
|
+
function get(url:string, handler:Handler,beforeRequest:MiddleWare[],afterRequest:MiddleWare[]){}
|
61
|
+
function post(url:string, handler:Handler,beforeRequest:MiddleWare[],afterRequest:MiddleWare[]){}
|
62
|
+
function put(url:string, handler:Handler,beforeRequest:MiddleWare[],afterRequest:MiddleWare[]){}
|
63
|
+
/*.....*/
|
64
|
+
/**
|
65
|
+
* 注册分组路由
|
66
|
+
*/
|
67
|
+
app.registerGroupRoute(groupRoute);
|
68
|
+
```
|
69
|
+
|
70
|
+
## 启动服务
|
71
|
+
```typescript
|
72
|
+
app.listen(port=80,callBack=()=>{});
|
73
|
+
```
|
74
|
+
|
75
|
+
## 已有中间件
|
76
|
+
1. **CorsMiddleware**
|
77
|
+
2. **LogMiddleware**
|
78
|
+
3. **StaticMiddleware**
|
79
|
+
4. **BodyParserMiddleware**
|
80
|
+
5. **validateMiddleware**
|
81
|
+
|
82
|
+
|
83
|
+
## 项目结构
|
12
84
|
```shell
|
13
|
-
|
14
|
-
|
85
|
+
qyani-web/
|
86
|
+
├── demo/
|
87
|
+
├── ├── index.js /** demo入口文件 **/
|
88
|
+
├── dist/
|
89
|
+
├── ├── lib/
|
90
|
+
├── ├── ├── middleware/
|
91
|
+
├── ├── ├── ├── body-parser.js /** body解析中间件 **/
|
92
|
+
├── ├── ├── ├── cors.js /** 跨域中间件 **/
|
93
|
+
├── ├── ├── ├── logger.js /** 日志中间件 **/
|
94
|
+
├── ├── ├── ├── static-resources.js /** 静态资源中间件 **/
|
95
|
+
├── ├── ├── ├── validate.js /** 参数验证中间件 **/
|
96
|
+
├── ├── ├── groupRoute.js /** 分组路由 **/
|
97
|
+
├── ├── index.js /** webApp入口文件 **/
|
98
|
+
```
|
99
|
+
|
package/dist/index.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
// app.js
|
2
2
|
import https from 'https';
|
3
3
|
import url from 'url';
|
4
|
-
import GroupRoute from "./lib/group-route";
|
4
|
+
import GroupRoute from "./lib/group-route.js";
|
5
5
|
import * as http from "http";
|
6
6
|
export { GroupRoute };
|
7
7
|
export default class WebApp {
|
@@ -225,6 +225,7 @@ export default class WebApp {
|
|
225
225
|
server = http.createServer(this._handleRequest.bind(this));
|
226
226
|
}
|
227
227
|
server.listen(port, callback);
|
228
|
+
console.log(`Server run on ${protocol}://127.0.0.1:${port}`);
|
228
229
|
}
|
229
230
|
/**
|
230
231
|
* 内部方法,用于注册路由
|