跳转到主要内容

标签(标签)

资源精选(342) Go开发(108) Go语言(103) Go(99) angular(82) LLM(78) 大语言模型(63) 人工智能(53) 前端开发(50) LangChain(43) golang(43) 机器学习(39) Go工程师(38) Go程序员(38) Go开发者(36) React(33) Go基础(29) Python(24) Vue(22) Web开发(20) Web技术(19) 精选资源(19) 深度学习(19) Java(18) ChatGTP(17) Cookie(16) android(16) 前端框架(13) JavaScript(13) Next.js(12) 安卓(11) 聊天机器人(10) typescript(10) 资料精选(10) NLP(10) 第三方Cookie(9) Redwoodjs(9) ChatGPT(9) LLMOps(9) Go语言中级开发(9) 自然语言处理(9) PostgreSQL(9) 区块链(9) mlops(9) 安全(9) 全栈开发(8) OpenAI(8) Linux(8) AI(8) GraphQL(8) iOS(8) 软件架构(7) RAG(7) Go语言高级开发(7) AWS(7) C++(7) 数据科学(7) whisper(6) Prisma(6) 隐私保护(6) JSON(6) DevOps(6) 数据可视化(6) wasm(6) 计算机视觉(6) 算法(6) Rust(6) 微服务(6) 隐私沙盒(5) FedCM(5) 智能体(5) 语音识别(5) Angular开发(5) 快速应用开发(5) 提示工程(5) Agent(5) LLaMA(5) 低代码开发(5) Go测试(5) gorm(5) REST API(5) kafka(5) 推荐系统(5) WebAssembly(5) GameDev(5) CMS(5) CSS(5) machine-learning(5) 机器人(5) 游戏开发(5) Blockchain(5) Web安全(5) Kotlin(5) 低代码平台(5) 机器学习资源(5) Go资源(5) Nodejs(5) PHP(5) Swift(5) devin(4) Blitz(4) javascript框架(4) Redwood(4) GDPR(4) 生成式人工智能(4) Angular16(4) Alpaca(4) 编程语言(4) SAML(4) JWT(4) JSON处理(4) Go并发(4) 移动开发(4) 移动应用(4) security(4) 隐私(4) spring-boot(4) 物联网(4) nextjs(4) 网络安全(4) API(4) Ruby(4) 信息安全(4) flutter(4) RAG架构(3) 专家智能体(3) Chrome(3) CHIPS(3) 3PC(3) SSE(3) 人工智能软件工程师(3) LLM Agent(3) Remix(3) Ubuntu(3) GPT4All(3) 软件开发(3) 问答系统(3) 开发工具(3) 最佳实践(3) RxJS(3) SSR(3) Node.js(3) Dolly(3) 移动应用开发(3) 低代码(3) IAM(3) Web框架(3) CORS(3) 基准测试(3) Go语言数据库开发(3) Oauth2(3) 并发(3) 主题(3) Theme(3) earth(3) nginx(3) 软件工程(3) azure(3) keycloak(3) 生产力工具(3) gpt3(3) 工作流(3) C(3) jupyter(3) 认证(3) prometheus(3) GAN(3) Spring(3) 逆向工程(3) 应用安全(3) Docker(3) Django(3) R(3) .NET(3) 大数据(3) Hacking(3) 渗透测试(3) C++资源(3) Mac(3) 微信小程序(3) Python资源(3) JHipster(3) 语言模型(2) 可穿戴设备(2) JDK(2) SQL(2) Apache(2) Hashicorp Vault(2) Spring Cloud Vault(2) Go语言Web开发(2) Go测试工程师(2) WebSocket(2) 容器化(2) AES(2) 加密(2) 输入验证(2) ORM(2) Fiber(2) Postgres(2) Gorilla Mux(2) Go数据库开发(2) 模块(2) 泛型(2) 指针(2) HTTP(2) PostgreSQL开发(2) Vault(2) K8s(2) Spring boot(2) R语言(2) 深度学习资源(2) 半监督学习(2) semi-supervised-learning(2) architecture(2) 普罗米修斯(2) 嵌入模型(2) productivity(2) 编码(2) Qt(2) 前端(2) Rust语言(2) NeRF(2) 神经辐射场(2) 元宇宙(2) CPP(2) 数据分析(2) spark(2) 流处理(2) Ionic(2) 人体姿势估计(2) human-pose-estimation(2) 视频处理(2) deep-learning(2) kotlin语言(2) kotlin开发(2) burp(2) Chatbot(2) npm(2) quantum(2) OCR(2) 游戏(2) game(2) 内容管理系统(2) MySQL(2) python-books(2) pentest(2) opengl(2) IDE(2) 漏洞赏金(2) Web(2) 知识图谱(2) PyTorch(2) 数据库(2) reverse-engineering(2) 数据工程(2) swift开发(2) rest(2) robotics(2) ios-animation(2) 知识蒸馏(2) 安卓开发(2) nestjs(2) solidity(2) 爬虫(2) 面试(2) 容器(2) C++精选(2) 人工智能资源(2) Machine Learning(2) 备忘单(2) 编程书籍(2) angular资源(2) 速查表(2) cheatsheets(2) SecOps(2) mlops资源(2) R资源(2) DDD(2) 架构设计模式(2) 量化(2) Hacking资源(2) 强化学习(2) flask(2) 设计(2) 性能(2) Sysadmin(2) 系统管理员(2) Java资源(2) 机器学习精选(2) android资源(2) android-UI(2) Mac资源(2) iOS资源(2) Vue资源(2) flutter资源(2) JavaScript精选(2) JavaScript资源(2) Rust开发(2) deeplearning(2) RAD(2)

category

Explore Hapi, Express.js, NestJS, Koa.js and Adonis.js to build your APIs in 2024

Introduction

Node.js has been the talk of town since 2009 and most of the backend developers tend to go for Node.js. Its popularity has been increased over the past few years.

It is known as the most popular web development tool in USA including clients like Netflix and PayPal.

The reason for the increase in popularity is the reduction in loading time and performance improvements. Therefore, it is crucial to analyze the top 5 Node.js Backend frameworks for 2024.

Hence, this article will cover the top 5 Node.js backend frameworks for 2024, their features, and common use cases.

Pst, if you want to check out a simple demo to build an Express API, checkout my Bit Scope.

Express.js: The Tested Champion

Express.js is one of the most famous backend frameworks of Node.js. It is an open-source web application framework, freely available and built upon the Node.js platform. Since it’s a minimal framework, both novice and seasoned web developers tend to go for Express.js. It is mainly used for creating web applications and RESTful APIs.

Key Features: What Makes It Stand Out?

1. Efficient Routing

Express.js offers a clean and simple method for managing various HTTP requests and assigning them to particular tasks. Let’s look at an example.

// app.js
const express = require('express');
const app = express();
const port = 3000;

// Route for Homepage
app.get('/', (req, res) => {
  res.send('Welcome to the homepage!');
});

// Route 2
app.get('/user/:id', (req, res) => {
  const userId = req.params.id;
  res.send(User Profile Page - ID: ${userId} );
});

2. Middleware Support

Express.js allows middleware support for handling HTTP requests. Let’s look at a simple example of creating a middleware for logging HTTP request details.

const express = require('express');
const app = express();
const port = 3000;

app.use((req, res, next) => {
  console.log([${new Date().toLocaleString()}] ${req.method} ${req.url} );
  next();
});

3. Easy Database Integration

Express.js is database-agnostic. It doesn’t enforce a specific database choice. Developers can choose their preferred database. The ease of integrating databases with Express.js is because of its modular and flexible nature and the rich ecosystem of npm packages that provide database connectivity.

4. Easy to learn

Express.js is known for its simplicity and minimalistic design, making it so easy for developers to learn, especially if they are already familiar with JavaScript and Node.js.

Plus, you can easily get started with Express.js with tools like Bit. If you’ve not used Bit before, it’s a next-generation build system for composable software.

And, if you think about it, Express.js on its own is composable in nature. You can plug and play components at any point in your app.

For example, you can create middleware components that you can plug in and out at any given time.

Figure: A Bit Scope of an Express API designed using Bit and Independent Components

You can see two components: an Authorizer as well as an API app. These two components have been implemented as independent Bit components and are maintained and versioned in its isolated space.

By doing so, you’re able to design your app in a composable manner, quickly!

To view this full implementation, check out my Bit Scope.

NestJS: A Modern and Structured Approach

NestJS is a framework that is known for building scalable and efficient Node.js server-side applications. It uses progressive JavaScript and has the capability of writing code in TypeScript. Even though it fully supports Typescript, it can write code in pure JavaScript and includes Object Oriented Programming, Functional Programming, and Functional Reactive Programming.

Key Features: What makes it stand out

1. Modularity

Nest.js allows code to be broken down into separate manageable modules which make it more maintainable. For an example, let’s look at the below module.

import { Module } from '@nestjs/common';

@Module({
 imports: [
  CacheModule
 ],
 controllers: [PaymentController],
 providers: [PaymentService],
})
export class PaymentModule {}

This payment module can be exported to other modules. In this example, we have exported common cache module inside this module. Since nest.js has a module structure, it makes it easy to maintain.

2. Scalable

Nest.js enables seamless scaling by breaking applications into manageable modules, supporting flexible component replacement, and accommodating high traffic through microservices and asynchronous operations. It ensures efficient handling of increased workload while maintaining reliability.

3. Dependency Injection

Dependency injection is simply the method of adding an external dependency to a class rather than creating it within the class itself. Let’s look at an example.

import {
 HttpException, Injectable, NotFoundException
} from '@nestjs/common';

@Injectable()
export class PaymentService {

 constructor() {}

 getReceipt() {
   return 'Payment Receipt';
 }

}

We have created payment service and added @Injectable() annotation to make it injectable. We can use the created service as stated below.

import { Controller, Get, Post, Body } from '@nestjs/common';
import { PaymentService } from './payment.service';
@Controller('payment')
export class PaymentController {
 constructor(private readonly paymentService: PaymentService) {}
@Get()
 getPaymentReceipt() {
 return this.paymentService.getReceipt();
 }
}

4. Type Safety

Nest.js uses TypeScript to provide type safety, which can be used to catch potential errors during development and improve code maintainability. Let’s look at an example.

export class PaymentDto {

  @IsNotEmpty()
  @IsEnum(SERVICE_PROVIDER_SLUG, {
    message: `Invalid serviceProvider. Valid options are: ${Object.values(SERVICE_PROVIDER_SLUG).join(', ')}`,
  })
  serviceProvider: string;

  @IsNotEmpty()
  @IsNumber()
  value: number;

  @IsNotEmpty()
  @IsString()
  validityPeriod: string;

  @IsNotEmpty()
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested()
  @Type(() => PaymentAttributesDto)
  paymentAttributes: PaymentAttributesDto[]

}

In this example, we have created a dto that includes multiple parameters and we have added annotations to validate the parameter type. For example, if we send a string value to the “value” parameter, it will throw an error.

Koa.js: Elegant and Lightweight

Koa.js is a smaller, more expressive web framework that is also designed by the Express.js team. It allows you to ditch callbacks and handle errors by leveraging async functions.

Key Features: What makes it stand out

1. Context Object (ctx)

Koa.js includes a functionality called ctx to capture request and response details. This context is passed to each middleware. In this example, we have logged method and request from the ctx object.

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx) => {
  const { method, url, request, response } = ctx;
  console.log('Method :' + method + ' Request : ' + request);
});

app.listen(3000);

2. Middleware Composition

Much similar to Express Js, Koa supports middleware functions for handling HTTP requests and responses. In this example, we have created a simple middleware.

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  await next(); 
});

app.listen(3000);

3. Async/Await Support

Koa uses the async/await syntax for writing asynchronous code in a more synchronous-looking way. The below example consists of using async/await keywords.

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx) => {
  const data = await fetchData();
  ctx.body = Data: ${data} ;
});

app.listen(3000);

4. Error Handling

Koa.Js supports various types of error handling. We can use app.emit() or ctx.throw() to handle errors. The below example consists of mentioned error-handling methods.

const koa = require('koa');
const app = new koa();

//Error Handling Method 1
app.use(async (ctx, next) => {
  try {
    await Promise.reject('Something went wrong');
  } catch (err) {
    ctx.status = err.status || 500;
    ctx.body = err.message;
    ctx.app.emit('error', err, ctx);
  }
});

//Error Handling Method 2
app.use(async (ctx, next) => {
  ctx.throw(500, 'error');
});

app.on('error', (err, ctx) => {
  console.log(err);
});

app.listen(3000);

Hapi.js

Hapi.js which is short for Http-API, is an open-source framework for developing scalable web applications. One of the most basic use cases of hapi is to build REST APIs.

Walmart Labs created hapi js to handle their traffic for events like Black Friday, one of the busiest days for online shopping in the U.S. calendar.

Key Features: What makes it stand out

1. Configuration-Driven Design

Using a configuration object, we are able to configure routes, settings and plugins in Hapi.js.

const Hapi = require('@hapi/hapi');

const server = Hapi.server({
  port: 3000,
  routes: {
    cors: true,
  },
});

server.route({
  method: 'GET',
  path: '/',
  handler: (request, h) => {
    return 'Hello, Hapi!';
  },
});

async function start() {
  await server.start();
  console.log(Server running at ${server.info.uri} );
}

start();

2. Powerful Plugin System

Hapi.js allows plugins to be easily integrated without much hassle. Let’s look at an example.

const start = async function () {

    const server = Hapi.server();

    await server.register([{
        plugin: require('plugin1'),
        options: {}
    }, {
        plugin: require('plugin2'),
        options: {}
    }]);
};

In this example, we have integrated two plugins. Options can be passed to the plugin using options key.

3. Authentication and Authorization

Hapi.js provides built-in support for various authentication strategies and allows developers to define access control policies easily.

server.route({
  method: 'GET',
  path: '/private-data',
  handler: (request, h) => {
    // Access private data only if authenticated
    const user = request.auth.credentials;
    return Welcome, ${user.username}! ;
  },
  options: {
    auth: 'jwt', // Use JWT authentication strategy
  },
});

Based on the example, we can directly define the authentication strategy as ‘jwt’.

4. Input Validation

Input validation is another crucial aspect of hapi.js. In the options object of a route, we can define which inputs need to be validated. Default validate object consists of below values.

{ 
   headers: true, 
   params: true, 
   query: true, 
   payload: true, 
   state: true, 
   failAction: 'error'
}

Adonis.js

Adonis.js is a full-featured MVC framework for Node.js. It has the capability of building scalable and maintainable applications. Adonis.js follows a similar structure to Laravel and includes features like ORM, authentication, and routing out of the box.

Key Features: What makes it stand out

1. Full-stack MVC framework

Adonis.js follows the MVC architectural pattern. Having a MVC framework helps organize code and makes it easier to maintain and scale.

2. Integrated ORM (Lucid) for database interactions

Adonis.js has its own ORM called Lucid. Lucid provides an expressive query builder and supports various database systems. In lucid, we can create models to read and write to the database. Let’s look at the below example.

const Model = use('Model')

class User extends Model {
}

module.exports = User

We are using this user model instead of database queries. Now we are creating a route, where inside we are fetching the users. We can simply user User.all() to fetch the users.

const Route = use('Route')
const User = use('App/Models/User')

Route.get('users', async () => {
return await User.all()
})

3. Authentication system

Adonis.js has built-in support for user authentication and authorization. It provides a set of methods and middleware for handling user sessions, password hashing, and access control.

Conclusion

In 2024, the above-mentioned backend frameworks stand tall in the market.

Whether you chose Express.js for its simplicity, Nest.js for its structure, Adonis.js for productivity, or Koa.js for the elegance, choosing the right framework is crucial.

It always depends on the requirement you have. It is crucial to understand what your project needs, and based on that, the appropriate framework should be selected.

Furthermore, it is essential to look for the latest trends, new features of existing frameworks, and new frameworks to have a successful backend development journey in 2024.

Learn More

A Modern Approach for Angular Development

blog.bitsrc.io

Monorepo, Poly-repo, or No Repo at all?

blog.bitsrc.io

A Modern Approach to React Development

blog.bitsrc.io

How we Build a Component Design System

blog.bitsrc.io

How We Build Micro Frontends

blog.bitsrc.io

Top 5 CSS Frameworks in 2024

blog.bitsrc.io

Creating a Shareable and Reusable Development Environment for Your Javascript Project

blog.bitsrc.io