The following content is trying to tell you how to upgrade to aex from expressjs, If you feel comfortable with aex new features.
If you want to :
- Use async/await directly.
- Simplify your code with decorator, helper.
- Programming web in the object oriented way.
- Avoid callback function
next
in your code, even when async/await is introduced. - Make you compatible with native node.js http server.
- To have a builtin error processing system.
- To have a builtin data filtering system.
- Don't want to write too much duplicated code.
- Want to focus your business logic.
- Aex is an upgrade in syntax with Typescript and ESNext.
- Aex extended the expressjs by replacing the callback function
next
with the data carrierscope
. - Aex can be programmed in the objected oriented way with decorators.
- 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;
}
}
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.
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));
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 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;
Aex is still on its early stage. You're risk at your own.