Skip to content

Latest commit

 

History

History
139 lines (105 loc) · 3.25 KB

upgrade from expressjs.md

File metadata and controls

139 lines (105 loc) · 3.25 KB

Upgrade to Aex From Expressjs

The following content is trying to tell you how to upgrade to aex from expressjs, If you feel comfortable with aex new features.

When your would upgrade from expressjs?

If you want to :

  1. Use async/await directly.
  2. Simplify your code with decorator, helper.
  3. Programming web in the object oriented way.
  4. Avoid callback function next in your code, even when async/await is introduced.
  5. Make you compatible with native node.js http server.
  6. To have a builtin error processing system.
  7. To have a builtin data filtering system.
  8. Don't want to write too much duplicated code.
  9. Want to focus your business logic.

What is the main differences between aex and expressjs?

  1. Aex is an upgrade in syntax with Typescript and ESNext.
  2. Aex extended the expressjs by replacing the callback functionnext with the data carrier scope.
  3. Aex can be programmed in the objected oriented way with decorators.
  4. Aex don't have cascaded routing system. Aex use flat routing system. All route must be specified absolutely.

Here is a simple example of how aex handler could be:

class UserLoginHandler {
  @post("/user/login")
  public async login(req, res, scope) {
    const { username, password } = scope.body;
  }
}

How to translate?

Route translate

For express code:

app.get(url, handler) should be translated to member function:

class Handler {
  @get(url)
  public handler() {}
}

All cascaded routes must be absolutized.

Middleware migration

Most express middlewares should be workable with aex by converting to aex middlewares with function toAsyncMiddleware.

Aex provides one function use and one decorator @inject to use middlewares through the web development process.

It use the same function use as express to take middleware.

const aex = new Aex();
aex.use(toAsyncMiddleware(yourMiddleware));

Middlewares can also be used with the @inject decorator.

const aex = new Aex();
aex.use(toAsyncMiddleware(yourMiddleware));

Simplify your process with decorators and helpers

Web handler can be secured, accelerated and simplified with decorators and helpers.

Here is an example:

class User {
  @post("/user/login")
  @body()
  @filter({
    body: {
      username: {
        type: string,
        minLength: 4,
        required: true,
      },
      password: {
        type: string,
        required: true,
      },
    },
    fallbacks: {
      body: errorInput,
    },
  })
  public async login(req, res, scope) {
    const { username, password } = scope.body;
  }

  @get("/user/profile")
  @session()
  @inject(logined, NotLoginFallback)
  public async login(req, res, scope) {
    const {
      session: { user },
    } = scope;
  }

  @get("/users")
  @query()
  @inject(paginate)
  public async login(req, res, scope) {
    const {
      pagination: { page, limit, offset },
    } = scope;
  }
}

Boot up

Boot up is one step more than express.

const aex = new Aex();

aex.push(User);
aex.prepare().start();

Click the links to know more about start and aex;

Disclaimer

Aex is still on its early stage. You're risk at your own.