From 7e41dcdc585703f029ccb9484bfd858b73d1d4e6 Mon Sep 17 00:00:00 2001 From: zenglingji Date: Fri, 15 Oct 2021 18:13:03 +0800 Subject: [PATCH] =?UTF-8?q?doc:=20=E8=A1=A5=E5=85=A8=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++++- packages/auf/README.md | 2 +- packages/core/README.md | 86 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6177df1..ea10ab4 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -# auf \ No newline at end of file +# 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) diff --git a/packages/auf/README.md b/packages/auf/README.md index b9d9a61..525bae7 100644 --- a/packages/auf/README.md +++ b/packages/auf/README.md @@ -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 diff --git a/packages/core/README.md b/packages/core/README.md index 51c6ecf..820d821 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -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. \ No newline at end of file