1.0.6 • Published 5 months ago

nesty-backend v1.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Nesty - Express Decorators for Clean Code

npm version TypeScript Express

Nesty-Backend is a lightweight library that brings decorator-based routing and middleware support to Express.js, making your backend code cleaner and more structured.


🚀 Features

  • Decorator-based routing (@Controller, @Get, @Post, etc.).
  • Middleware decorators (@Middleware) for request processing.
  • Automatic route registration.
  • Fully typed with TypeScript for better DX.

📦 Installation

Install the package via npm:

npm install nesty-backend

or using yarn:

yarn add nesty-backend

📖 Usage

1️⃣ Create a Controller

import { Controller, Get } from "nesty";
import { Request, Response } from "express";

@Controller("users")
class UserController {
  @Get("/profile")
  getProfile(req: Request, res: Response) {
    res.json({ message: "User profile data" });
  }
}

export default UserController;

2️⃣ Register Controllers in Express

import express from "express";
import { registerControllers } from "nesty";
import UserController from "./controllers/UserController";

const app = express();
registerControllers(app, [UserController]);

app.listen(3000, () => console.log("Server running on port 3000"));

3️⃣ Add Middleware

import { Middleware, Controller, Get } from "nesty";
import { Request, Response, NextFunction } from "express";

const AuthMiddleware = (req: Request, res: Response, next: NextFunction) => {
  if (!req.headers.authorization) {
    return res.status(401).json({ message: "Unauthorized" });
  }
  next();
};

@Controller("users")
class UserController {
  @Get("/profile")
  @Middleware(AuthMiddleware)
  getProfile(req: Request, res: Response) {
    res.json({ message: "User profile data" });
  }
}

📌 API Reference

Controller Decorator

@Controller("prefix")

Defines a controller with a base route.

Route Decorators

@Get("path")
@Post("path")
@Put("path")
@Delete("path")

Registers an HTTP method for a route.

Middleware Decorator

@Middleware(middlewareFunction)

Attaches middleware to a specific route.

registerControllers Function

registerControllers(app, [UserController]);

Registers controllers with Express.


📜 License

This project is licensed under the MIT License. See the LICENSE file for details.


⭐ Show Your Support

If you like this project, give it a ⭐ on GitHub!

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago