项目作者: BrokenSwing

项目描述 :
Decorators for express applications
高级语言: TypeScript
项目地址: git://github.com/BrokenSwing/exwide.git
创建时间: 2020-03-25T18:27:34Z
项目社区:https://github.com/BrokenSwing/exwide

开源协议:

下载


ExWiDe

Node.js CI

logo

Exwide is a framework that aims to simplify projects built using express
and TypeScript. It takes advantage of TypeScript decorators and typing system to guess the format
you want the user input to be converted to and validates it to match your needs.

It also allows to design your project in a Controller/Service way with dependency injection
(coming ASAP).

Table of content

Setting up a project

Install the framework as a dependency

Coming soon

Configure TypeScript

To use the framework you must configure your tsconfig.json with the three following options:

  1. {
  2. "compilerOptions": {
  3. "target": "ES2019",
  4. "experimentalDecorators": true,
  5. "emitDecoratorMetadata": true,
  6. }
  7. }

The first one allows to use Array#flatMap and the two other ones allow the usage of decorators.

Some example projects can be found in the example folder.

Getting started

You need to have a setup project to continue. If you haven’t, follow these steps.

Create a controller

The first step is to create a controller. A controller is class decorated with the @Controller
decorator.

  1. import {Controller} from 'exwide';
  2. @Controller('/hello')
  3. class HelloController {
  4. }

This is the simplest controller you can create. It just does nothing.

Declare a request handler in a controller

Let’s add a request handler to handles GET requests on URL /hello and returns Hello, world!.
A request handler is a class method defined in a controller. This one is decorated with one
of specialized handler decorators (@Get, @Post, etc …).

Let’s create the handler :

  1. import {Controller, Get} from 'exwide';
  2. @Controller('/hello')
  3. class HelloController {
  4. @Get('/')
  5. helloWorld(): string {
  6. return 'Hello, world!';
  7. }
  8. }

Bootstrap the framework to run the application

Now we have defined a request handler, we can simply run the application. You can
use the bootstrap function and pass the controller as an argument to start the application.

  1. import {Controller, Get, bootstrap} from 'exwide';
  2. @Controller('/hello')
  3. class HelloController {
  4. @Get('/')
  5. helloWorld(): string {
  6. return 'Hello, world!';
  7. }
  8. }
  9. bootstrap(HelloController).then((app) => {
  10. app.listen(8000, () => console.log('Visit http://localhost:8000/hello/'));
  11. }).catch(() => console.error('Unable to start the application'));

You can now visit http://localhost:8000/hello/ and you’ll see
a page returning Hello, world!.

Add an user provided parameter

We now want to display Hello, George! if a GET request is made on /hello/George, or
to any other name. Let’s add a parameter that matches the name.

  1. import {Controller, Get, bootstrap, RouteParam} from 'exwide';
  2. @Controller('/hello')
  3. class HelloController {
  4. @Get('/')
  5. helloWorld(): string {
  6. return 'Hello, world!';
  7. }
  8. @Get('/:name')
  9. helloAnyone(@RouteParam('name') userName: string): string {
  10. return `Hello, ${userName}!`;
  11. }
  12. }
  13. bootstrap(HelloController).then((app) => {
  14. app.listen(8000, () => console.log('Visit http://localhost:8000/hello/George'));
  15. }).catch(() => console.error('Unable to start the application'));

Now you can visit http://localhost:8000/hello/George and
the message Hello, George! will be displayed. The framework finds the name parameter in the
route (the parsing is done by express of course) then pass it to our method.

Now, we want to display n times the messages, the n value being passed by user. We want
the URL /hello/George/?times=5 to display :

  1. Hello, George!
  2. Hello, George!
  3. Hello, George!
  4. Hello, George!
  5. Hello, George!

Let’s do this :

  1. import {Controller, Get, bootstrap, RouteParam, QueryParam} from 'exwide';
  2. @Controller('/hello')
  3. class HelloController {
  4. @Get('/')
  5. helloWorld(): string {
  6. return 'Hello, world!';
  7. }
  8. @Get('/:name')
  9. helloMultiple(@RouteParam('name') userName: string, @QueryParam('times') n: number): string {
  10. let result = '';
  11. for (let i = 0; i < n; i++) {
  12. result += `Hello, ${userName}!<br>`;
  13. }
  14. return result;
  15. }
  16. @Get('/:name')
  17. helloAnyone(@RouteParam('name') userName: string): string {
  18. return `Hello, ${userName}!`;
  19. }
  20. }
  21. bootstrap(HelloController).then((app) => {
  22. app.listen(8000, () => console.log('Visit http://localhost:8000/hello/George?times=5'));
  23. }).catch(() => console.error('Unable to start the application'));

(take care a method placement, they’re tested in declaration order)

Visit http://localhost:8000/hello/George?times=5,
you should see the appropriate message printed 5 times.