ExpressWebJs 4.2 is Here With New Features!

Written by futurecode | Published 2023/07/19
Tech Story Tags: expresswebjs | expresswebjs-release | expresswebjs-new-release | expresswebjs-release-note | expresswebjs-version-4.2 | expresswebjs-latest-version | typeorm-swagger-grpc | best-nodejs-framework

TLDRThe ExpressWebJs team is pleased to release version 4.2 which supports Typescript 5.0 and above. This new version comes with some amazing features like Multi threading and Easy OpenAPI integration, TypeORM integration, optimised Events and Listeners etc.via the TL;DR App

ExpressWebJs 4.2 is here, and it's packed with new features that will make your development process faster, easier, and more efficient.

With support for Typescript 5.0 and above, you can now build more robust and maintainable applications. The new TypeORM data source makes it easy to connect to your database, and the Easy OpenAPI integration makes it a breeze to generate API documentation.

If you're looking to build high-performance, scalable microservices, then gRPC integration is for you. And with the multithreading optimizations, you can now handle more requests simultaneously without sacrificing performance.

The database connection and configuration optimizations make it easier to configure your database connections, and the events and listeners optimizations improve the performance of your events and listeners

Below is a quick run-through of the features:

  • TypeORM data source,
  • Easy OpenAPI integration,
  • gRPC integration,
  • Other optimizations on
    • Multithreading
    • Database connection and configuration
    • Events and listeners

https://twitter.com/EmekaIgbokwe/status/1675489039015616515?embedable=true

TypeORM integration:

TypeORM is an ORM framework for TypeScript and JavaScript that provides a rich set of features for working with databases. It supports various database management systems, including MySQL, PostgreSQL, and MongoDB. TypeORM also offers features like entity modeling, migrations, and query building.

Integrating TypeORM in ExpressWebJs is a straightforward process. This can help streamline your application's database interactions.

import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
import { ITodoModel, TodoStatus } from "./Types/ITodoModel";

@Entity("todos")
export class TodoModel implements ITodoModel {
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  @Column("varchar", { length: 600 })
  name!: string;

  @Column({
    type: "enum",
    enum: TodoStatus,
    default: TodoStatus.PENDING,
  })
  isComplete!: TodoStatus;

  @Column({ type: "timestamptz", default: () => "CURRENT_TIMESTAMP(6)" })
  created_at?: Date;

  @Column({ 
    type: "timestamptz", default: () => "CURRENT_TIMESTAMP(6)", 
    onUpdate: "CURRENT_TIMESTAMP(6)" 
  })
  updated_at?: Date;
}

TypeORM repository can also be created to help interact with the model.

import { TodoModel } from "App/Model/TodoModel";
import { ITodoModel } from "App/Model/ITodoModel";
import { TypeORMRepository } from "Elucidate/Repository/TypeORM";

export class TodoRepository extends TypeORMRepository<ITodoModel> {
  constructor() {
    super(TodoModel);
  }
}

Visit ExpressWebjs TypeORM integration to learn more

Related Article: Learn How to Use TypeORM With ExpressWebJs to Create a Backend Service

Easy OpenAPI integration:

The OpenAPI specification is a standard way to describe RESTful APIs. It defines a set of terms and definitions for describing the structure of an API, as well as the operations that can be performed on it. This makes it easy for humans and machines to understand how an API works.

Using ExpressWebJs and OpenAPI together, you can easily create RESTful APIs that are both well-documented and easy to use. This can help you to improve the discoverability and usability of your APIs, which can lead to increased adoption and usage.


To begin using it, we first install the required dependency.

npm install swagger-ui-express

Head over to Config/Swagger.ts to configure Swagger:

export default {
  info: {
    title: env("APP_NAME"),
    version: "1.0",
    description: "My website API docs with ExpressWebJs and Swagger",
    license: {
      name: "MIT",
      url: "https://spdx.org/licenses/MIT.html",
    },
    contact: {
      name: "",
    },
  },
  host: "127.0.0.1:5100",
  basePath: "/api",
};


Once the installation process is complete, open app.ts in the root directory and enable Swagger.

StartApp.withHttpServer({ useSwagger: true });

After setting the useSwagger to true, Swagger can now be configured in the booted method in the App Service Provider class located in the App/Providers directory.

import { ServiceProvider } from "Elucidate/Support/ServiceProvider";
import SwaggerUI from "swagger-ui-express";
import swaggerOutPut from "../../Resources/swagger-output.json";

export class AppServiceProvider extends ServiceProvider {
  /**
   * Load any service after application boot stage
   * @return void
   */
  public async booted() {
    if (this.isEnvironment("development")) {
      const app = this.use("Application");
      app.use("/api/docs", SwaggerUI.serve, SwaggerUI.setup(swaggerOutPut));
    }
  }
}


Now that swagger has been configured in the App Service Provider, we can now start documenting our APIs.

Route.get("/welcome", (req: Request, res: Response) => {
  //#swagger.description = "Welcome Endpoint";
  res.send("Welcome to ExpressWebJs");
});

gRPC integration:

This is one of the amazing features added to ExpressWebjs. To start using gRPC, you need to start your app with gRPC like this in the app.ts file.

/*
|---------------------------------------------------------------
| Http Server
|---------------------------------------------------------------
| This file bootstraps ExpressWebJs to start the Http server.
| Application Host, Port and Transfer Protocols are configured
| in the .env file. You are free to configure them. 
|
*/
import { StartApp } from "expresswebcorets";

StartApp.withGRPC();

Create proto file:

syntax = "proto3";
package Todo;
service TodoService {
    rpc GetAllTodos(Void) returns (TodoList) {}
}
message Void{}
message TodoModel {
    string     id = 1;
    string     name = 2;
    string     isCompleted=3;
    string  created_at = 4;
    string  updated_at = 5;
}
message TodoList {
    bool       status = 1;
    string     message = 2;
    repeated   TodoModel  data = 3;
}

Once the proto file is created, you will add the path to the gRPC config file in the Config folder in the root directory.

import { GrpcRequest } from "Elucidate/Support/GRPC/GrpcRequest";
import { GrpcResponse } from "Elucidate/Support/GRPC/GrpcResponse";
import { Path } from "expresswebcorets/lib/Utils/Path";
export { GrpcRequest, GrpcResponse };

export default {
  /*
  |--------------------------------------------------------------------------
  | gRPC Proto files
  |--------------------------------------------------------------------------
  | gRPC uses a contract-first approach to API development and Protocol
  | buffers (protobuf) are used as the Interface Definition Language (IDL).
  | Here you specify the path to your proto files
  |
  */
  protos: [Path("App/Protos/Todo.proto")], 👈 // Path to Todo.proto file.
  //this accepts array of files

  /*
  |--------------------------------------------------------------------------
  | gRPC Configuration option
  |--------------------------------------------------------------------------
  | Here you define gRPC Configuration Option
  */

  options: {
    keepCase: true,
    longs: String,
    enums: String,
    arrays: true,
    defaults: true,
    oneofs: true,
  },
};

Visit ExpressWebJs gRPC integration for complete documentation and setup.

Other optimizations:

There have been some optimizations to the following APIs.

  • Multi-threading API:
  • Database connection and configuration
  • Events and listeners

So what are you waiting for? Upgrade to ExpressWebJs 4.2 today and start building better applications!


Written by futurecode | Software Engineer
Published by HackerNoon on 2023/07/19