sindresorhus / awesome-nodejs

:zap: Delightful Node.js packages and resources
https://node.cool
Creative Commons Zero v1.0 Universal
57.56k stars 5.81k forks source link

add @egomobile/http-server in "Web frameworks" #1180

Closed ghost closed 2 years ago

ghost commented 2 years ago

@egomobile/http-server

This is a module that is very similar to Express.js and offers most of its known API.

Apart from the well-known methods, such as get() or post(), well-known middlewares, such as cors(), can also be included.

import cors from "cors";  // npm i && npm i -D @types/cors
import createServer from "@egomobile/http-server";

const app = createServer();

// use with "cors" middleware
app.get("/", [ cors() ], async (request, response) => {
  // all handlers are Promise-optimized, so you do not need a
  // response.end() anymore
});

app.post("/", [ cors() ], async (request, response) => {
  // do your POST here
});

await app.listen(8080);

In addition, a lot of useful middlewares are already built in.

import createServer, { json }  from "@egomobile/http-server";

// ...

app.post("/", [ json() ], async (request, response) => {
  // now you should have a parsed and valid
  // JavaScript object in request.body
  const { body } = request;
});

// ...

The server is especially optimized for the use in the microservice area, but can also be used like Express for monolith backends or websites.

The library is also characterized by the fact that you can implement your endpoints extremely fast with the help of decorators and it is also possible to realize schema validations in the simplest way with the help of Joi or to provide API documentation as Swagger UI.

// path inside project folder: /controllers/@abc/index.ts

import {
  Controller,
  ControllerBase,
  DELETE,
  GET,
  POST,
  IHttpRequest,
  IHttpResponse,
} from "@egomobile/http-server";

// base path: /:abc
//            each @ prefix in a directory's base name will be converted to a parameter
//            which can be accessed via 'params' prop of a 'request' context instance
//            of a method
@Controller()
export default class WithParamsController extends ControllerBase {
  @GET() // full path is: /:abc
  //
  // 'index' as name of the method will also be handled an empty string
  // and we do not specify an explicit (sub)path in '@GET()'
  async index(request: IHttpRequest, response: IHttpResponse) {
    response.write("value of 'abc' is: " + request.params!.abc);
  }

  @POST() // full path is: /:abc/foo
  //
  // the name of method ('foo') is different to 'index'
  // and we do not specify an explicit (sub)path in '@POST()'
  async foo(request: IHttpRequest, response: IHttpResponse) {
    response.write("value of 'abc' is: " + request.params!.abc);
  }

  @DELETE("/baz/:buzz") // full path is: /:abc/baz/:buzz
  //
  // the name of method ('bar') is ignored here
  // because we specified an explicit (sub)path in '@DELETE()'
  //
  // this will also happen, even if the method name is 'index'
  async bar(request: IHttpRequest, response: IHttpResponse) {
    response.write("value of 'abc' is: " + request.params!.abc);
    response.write("value of 'buzz' is: " + request.params!.buzz);
  }
}

An instance is between 3-4 faster than Express because, among other things, it dispenses with most of its auxiliary functions to achieve optimum speed.

A full overview provides the README and the projects's wiki

sindresorhus commented 2 years ago

The submitted project should be more than 30 days old and the repo should have at least 60 stars. - https://github.com/sindresorhus/awesome-nodejs/blob/main/contributing.md