Skip to content

Commit

Permalink
Merge pull request #4 from guilhermemj/release-next
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
guilhermemj committed Aug 8, 2020
2 parents a14db51 + 8adddcf commit 4289bad
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ A simple and opinionated web server powered by [Express](http://expressjs.com/).
- [Typescript example](#typescript-example)
- [Options](#options)
- [`httpPort`](#httpport)
- [`parserOptions`](#parseroptions)
- [`corsOptions`](#corsoptions)
- [`routes`](#routes)
- [`beforeEach`](#beforeeach)

Expand Down Expand Up @@ -74,7 +76,7 @@ const webServer = new WebServer({

## Options

`WebServer` constructor may receive an object with `httpPort`, `routes` and `beforeEach` properties.
`WebServer` constructor accepts an object with the following properties:

### `httpPort`

Expand All @@ -83,6 +85,20 @@ const webServer = new WebServer({

The HTTP port that the server will listen to.

### `parserOptions`

- **Type:** `BodyParserOptions`.
- **Default:** `undefined`.

Options object passed to [express.json](https://expressjs.com/pt-br/api.html#express.json).

### `corsOptions`

- **Type:** `CorsOptions`.
- **Default:** `undefined`.

Options object passed to [cors](https://www.npmjs.com/package/cors).

### `routes`

- **Type:** `Array<Route>`.
Expand All @@ -93,6 +109,6 @@ Your application route definitions. Details will be described below.
### `beforeEach`

- **Type:** `Controller`.
- **Default:** `null`.
- **Default:** `undefined`.

Middleware that should run before each route controller. It's usually used for logging purposes.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guilhermemj/micro-web-server",
"version": "1.1.1",
"version": "1.1.2",
"description": "A simple and opinionated web server powered by Express",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ class WebServer {

this.app = express();

this.app.use(cors());
this.app.use(express.json());
this.app.use(cors(opt.corsOptions));
this.app.use(express.json(opt.parserOptions));

if (Array.isArray(opt.routes)) {
opt.routes.forEach(route => this.registerRoute(route));
this.registerRoutes(opt.routes);
}
}

registerRoutes(routes: Array<Route>): void {
routes.forEach(route => this.registerRoute(route));
}

registerRoute(route: Route): void {
const {
path,
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ErrorRequestHandler, RequestHandler } from 'express';
import { Options as BodyParserOptions } from 'body-parser';
import { CorsOptions } from 'cors';

export type Controller = RequestHandler | ErrorRequestHandler;

Expand All @@ -13,6 +15,8 @@ export interface Route {

export interface ServerOptions {
httpPort?: number;
parserOptions?: BodyParserOptions;
corsOptions?: CorsOptions;
routes?: Array<Route>;
beforeEach?: Controller;
}

0 comments on commit 4289bad

Please sign in to comment.