Code Monkey home page Code Monkey logo

express-backend-template's Introduction

Express Backend Template

GitHub license Backend Framework Backend Framework

A template for kickstarting your Express.js backend development. This template provides a structured setup for controllers, routes, and middleware, making it easy to organize and scale your backend applications.

Table of Contents

Features

  • Controller and Route Decorators: Easily define controllers and routes using decorators for cleaner and more maintainable code.
  • Controller Factory: Manage and create instances of controllers using a centralized factory to ensure single instances.
  • HTTP Method Decorators: Convenient decorators (@Get, @Post, @Delete, @Put, @Patch) for defining routes with specific HTTP methods.
  • Express App Setup: Includes a basic setup for an Express.js application with CORS, logging, and JSON body parsing.
  • MIT License: Use, modify, and distribute the template freely under the terms of the MIT license.

Getting Started

Prerequisites

  • Node.js and npm installed on your machine.

Installation

  1. Clone the repository:

    git clone https://github.com/coderoyalty/express-backend-template.git
  2. Navigate to project directory:

    cd express-backend-template
  3. Install dependencies

    npm install

    or using yarn

    yarn install
    

Usage

Let's create a TodoController to showcase how to use this template.

// File: controllers/todo.ts
import BaseController from "@/controllers/base.controller";
import Controller from "@utils/controller.decorator";
import {Get, Post} from "@utils/route.decorator";

// mimick a database
const todos = [
   {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  },
  {
    "userId": 2,
    "id": 3,
    "title": "fugiat veniam minus",
    "completed": false
  },
  {
    "userId": 2,
    "id": 4,
    "title": "et porro tempora",
    "completed": true
  },
  {
    "userId": 3,
    "id": 5,
    "title": "laboriosam mollitia et enim quasi adipisci quia provident illum",
    "completed": false
  },
]

let nextId = todos.length

Let's create a TodoController class

@Controller()
export class TodoController extends BaseController {
  constructor() {
    super("/users/:id/todos");
  }
}

The @Controller() registers the TodoController to the main application. Now, let's create two methods: fetchAll and createTodo. the first method handles a GET - /api/users/:id/todos, while the latter handles POST - /api/users/:id/todos.

@Get('/')
async fetchAll(req: express.Request, res: express.Response) {
  const { id } = req.params;
  const data = todos.filter((todo) => todo.userId === parseInt(id));
  res.json({
    data,
    count: data.length,
  });
}

 @Get("/:todoId")
  async fetch(req: express.Request, res: express.Response) {
    const { todoId, id } = req.params;
    const todo = todos.find(
      (todo) =>
        todo.id === parseInt(todoId) && todo.userId === parseInt(id)
    );

    if (!todo) {
      return res.sendStatus(404);
    }

    res.json(todo);
  }

@Post('/')
async createTodo(req: express.Request, res: express.Response) {
  const { id } = req.params;
  const { title } = req.body;
  const data = {
    userId: parseInt(id),
    id: ++nextId,
    title,
    completed: false,
   }
   todos.push(data);
   return res.status(201).json(data);
};

These decorators: @Get and Post register a method to the controllers' primary router. This makes it possible to add new request handlers without modifying multiple lines of code. The name of these decorators reflects on the HTTP method they assign a method to.

Now, let's add an import statement for TodoController in /controllers/index.ts:

//.. controllers/index.ts
import "@/controllers/base.controller";
import "@/controllers/todo"; // import our new file

This ensures that our module is loaded.

Now you can run the application

npm run start

and make requests to these endpoints:

  • GET - /api/users/:id/todos
  • GET - /api/users/:id/todos/:todoId
  • POST - /api/users/:id/todos

Check the todo example branch for more implementation details.

Task: Add a request handler for GET - /api/users/:id/todos/:todoId

express-backend-template's People

Contributors

coderoyalty avatar dependabot[bot] avatar

Watchers

 avatar

express-backend-template's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.