DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
NotFound404
NotFound404

Posted on

Create Your Web Server with @http ( aex Intro I)

With decorators into stage 2. You will see and use more and more decorators in your js/nodejs projects.

With the help from decorators, we can create a web server in a more simple but powerful way.

In aex, @http was introduced to mimic HTTP Methods and corresponding requested url.

Here are the steps on how to use @http to create a web server.

1. Define a handler with @http.

For example we need to write a user login page, you can simply create such a handler with the @http decorator.

import { http } from "@aex/core";

class User {
  @http("get", "/user/login")
  login() {
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Create an Aex object

After created a class, we need push this class to an aex object to initialize handlers the class defined.
Here is the code:

const aex = new Aex();
// You can push a lot of classes here
// aex.push(Class0);
// ...
// aex.push(ClassN);
// Here only one class( the User class ) is ok
aex.push(User);
// prepare classes for http requests
aex.prepare();
Enter fullscreen mode Exit fullscreen mode

3. Start a server

After the preparation, aex has loaded all request handlers from classes.
Now we can start the server to receive requests and handle them.

If you are in a none-async function,
You may use this one to start the server:

aex.start(8080).then();
Enter fullscreen mode Exit fullscreen mode

or You can use this one:

await aex.start(8080);
Enter fullscreen mode Exit fullscreen mode

It will return an server, you can store it if you need it later.

Now a very simple server is ready for you.

You can start to write your handle now.

Top comments (0)

🌚 Life is too short to browse without dark mode