Skip to content

Commit

Permalink
doc: 补全文档
Browse files Browse the repository at this point in the history
  • Loading branch information
zenglingji committed Oct 15, 2021
1 parent ad90e21 commit 7e41dcd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# auf
# auf

This is a simple HTTP framework write in Typescript with Node.js API.

It's a monorepo project, which contains packages below:


- [@vergiss/auf-core](!https://github.com/divasatanica/auf/tree/main/packages/core)
- [@vergiss/auf-middlewares](!https://github.com/divasatanica/auf/tree/main/packages/middlewares)
- [@vergiss/auf-helpers](!https://github.com/divasatanica/auf/tree/main/packages/helpers)
- [@vergiss/auf-template-engine](!https://github.com/divasatanica/auf/tree/main/packages/template-engine)
2 changes: 1 addition & 1 deletion packages/auf/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@vergiss/auf`

This is a simple http framework implemented with Typescript and Node.js raw API.
This is a simple http framework written in Typescript with Node.js raw API.

## Feature

Expand Down
86 changes: 81 additions & 5 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,87 @@
# `core`
# `@vergiss/auf-core`

This is a simple http framework written in Typescript with Node.js raw API.

## Feature

- Router Middleware supports schema string and regular expression matching.
- Custom asynchornous middlewares.
- Static resources serving with GUI.
- Run in cluster mode.

## Installation

> npm i @vergiss/auf
or

> yarn add @vergiss/auf
> TODO: description

## Usage

```
const core = require('core');
```js
const { Server: StaticServer, Middlewares, Router, RouterMapFactory } = require('@vergiss/auf');

const port = 5000;
const timeout = 3000;
const callback = () => {
console.log('Static Server Listening on', port);
};
const errorHandler = e => {
console.error('[static-server] Error:', e.message);
};

const server = new StaticServer({
port,
assetsRoot: path.resolve(__dirname, '../public'),
workerNum: 8,
});

const routerMap = RouterMapFactory();

// Define routes and handlers
routerMap.get('/hello', async (ctx, next) => {
ctx.body = 'yes!!!!!';
await next(ctx);
});

// TODO: DEMONSTRATE API
// Define middlewares

server.applyMiddleware([
Middlewares.ErrorBoundary({ errorHandler }),
Middlewares.Timeout({ timeout }),
Middlewares.Logger(console),
Middlewares.AuthControl({
whitelist: [
'/'
]
}),
Middlewares.CacheControl(),
Middlewares.StaticRoutes({}),
Router()
]);

try {
server.setup(callback);
} catch (e) {
console.error(e.message);
}
```

## Middlewares

For more scalability, the framework itself only provide a container for middelwares and drive the context to flow through them, which makes middlewares able to modify the context or make some operation. So you can see the basic functionalities are also implemented as a middleware. It is called Onion Style Middleware, which can provide more convenient access to the context.

For the options details, you can see [Docs of built-in middlewares](!https://github.com/divasatanica/auf/blob/main/packages/middlewares/README.md).



## Router and routes

It supports two style for route definition:

1. Schema string matching like `'/api/getUser/:id'` or plain text string `'/api/echo'`
2. Regular expression matching.

In schema string matching style, the `ctx.params` will be an object; In regular expression matching style, the `ctx.params` will be an array.

0 comments on commit 7e41dcd

Please sign in to comment.