ECN>> eec>> 返回
项目作者: bill-ahmed

项目描述 :
Define ExpressJS routes as Typescript classes.
高级语言: TypeScript
项目地址: git://github.com/bill-ahmed/eec.git
创建时间: 2020-12-19T15:23:22Z
项目社区:https://github.com/bill-ahmed/eec

开源协议:

下载


" class="reference-link">eec (express-es6-classes)
Test Suite

Define ExpressJS routes as Typescript classes using decorators. Inspired by the MVC pattern.

Install:

  1. npm install eec --save

This package makes use of decorators and reflect-metadata. so you will also need to enable the following flags in your tsconfig.json:

  • "experimentalDecorators": true
  • "emitDecoratorMetadata": true

Usage:

Here’s a basic example to get started. Note that buildController(...) returns a valid Express Router instance that can be used directly with app.use(...).

  1. import express from 'express';
  2. import { route, get, post, buildController, BaseController } from 'eec';
  3. class DashboardController extends BaseController {
  4. static PATH = '/dashboard'
  5. @route({ index: true }) // Responds to: /dashboard. `index` says to use as index route
  6. async index() {
  7. this.response.status(200).send(`Welcome to the dashboard!`);
  8. }
  9. @get() // Responds to /dashboard/users
  10. async users() {
  11. // Also req.params & req.query
  12. if(this.params.someParameter || this.query.anotherParameter) {
  13. this.response.json({ name: 'John' });
  14. return;
  15. }
  16. this.response.sendStatus(404)
  17. }
  18. @post(':id') // Responds to /dashboard/updateUser/<user_id>
  19. async updateUser() {
  20. if(this.params.id === 'admin') {
  21. this.response.sendStatus(403);
  22. }
  23. else {
  24. this.response.status(200).send('Success!');
  25. }
  26. }
  27. }
  28. const app = express();
  29. app.use(buildController(DashboardController))
  30. // Go to http://localhost:3000/dashboard/
  31. app.listen(3000, () => { console.log("App listening!") })

The @route() decorator supports many more configurations. Take a look at the src/examples directory to get started with more scenarios.

Running Examples

To run all the examples provided:

  1. Clone this repo
  2. npm install
  3. npm start

A server will be listening at port 3000!

Running tests

  1. npm install
  2. npm test

More Example Usages

Use helper methods defined in the class

  1. class DashboardController {
  2. static PATH = '/dashboard'
  3. // Responds to: /dashboard/users. The function name is taken to be the endpoint!
  4. @route()
  5. async users(req, res) {
  6. await this.doSomeStuff();
  7. res.json({ name: 'John', age: 34 })
  8. }
  9. // All method NOT marked as @route() are ignored, but are still avilable in each route.
  10. async doSomeStuff() { console.log('Working!') }
  11. }

Define HTTP method types explicitly:

  1. // Don't want a GET route? Specify a different one!
  2. @route({ type: 'post' })
  3. async updateUsers(req, res) { ... }
  4. // Or use one of the several aliases
  5. @post()
  6. async updateUsers(req, res) { ... }
  7. // Respond to multiple HTTP methods
  8. @route({ type: ['post', 'put', 'delete'] })
  9. async updateRecords(req, res) { ... }

Multiple middleware per route:

  1. @route({ middleware: [Logger, Authentication] })
  2. async updateData(req, res) { ... }

Define middleware for all routes:

  1. @useMiddleware(Logger) //Pass in array for multiple
  2. class DashboardController {
  3. static PATH = '/dashboard'
  4. // This middleware will run AFTER the Logger!
  5. @route({ middleware: Authentication })
  6. async users(req, res) { ... }
  7. }
  8. export default buildController(DashboardController)

Request/Response and other objects are also provided inside this context!

  1. class DashboardController {
  2. static PATH = '/dashboard'
  3. @get()
  4. async users() {
  5. let raw = this.query.myNum
  6. let num = Number.parseInt(raw)
  7. this.response.json({ result: num })
  8. /** There are aliases provided for:
  9. *
  10. * req --> this.request
  11. * res --> this.response
  12. * next --> this.next,
  13. *
  14. * res.locals --> this.locals
  15. * req.params --> this.params
  16. * req.query --> this.query
  17. *
  18. * These can be used in any number of
  19. * helper functions within the class!
  20. */
  21. }
  22. }
  23. export default buildController(DashboardController)