DEV Community

Cover image for Write Express Server in Class Based or Object Oriented Way.
Muhammad Semeer
Muhammad Semeer

Posted on

3 2

Write Express Server in Class Based or Object Oriented Way.

We have seen lot's of examples of express server function based approaches. So I just created a simple express server with OOPs. Checkout

Now We can use express in Class Based Approach

Installation

npm i express-oops
Enter fullscreen mode Exit fullscreen mode

Also make sure that you have Node.js 14 or newer in order to use it.

Create A Server

const { Server } = require('express-oops');

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

const server = new Server(app, 3000);

server.start();
Enter fullscreen mode Exit fullscreen mode

This will start a server on port 3000.

Create A Controller

const { Server, Controller, Methods } = require("express-oops");
const express = require("express");

const server = new Server(express(), 3000);

class IndexController extends Controller {
  path = "";
  routerMiddleWares = [];

  routes = [
    {
      method: Methods.GET,
      path: "/",
      handler: this.index,
    },
  ];

  index(req, res) {
    res.send("Hello World!");
  }
}

server.start(() => console.log("Server started"));
server.loadControllers([new IndexController()]);
Enter fullscreen mode Exit fullscreen mode

Add Middlewares

  1. Global Middlewares
    This middleware are invoked on every request.

    const { Server, Controller, Methods } = require("express-oops");
    const express = require("express");
    
    const server = new Server(express(), 3000);
    
    class IndexController extends Controller {
    path = "";
    routerMiddleWares = [];
    
    routes = [
        {
        method: Methods.GET,
        path: "/",
        handler: this.index,
        },
    ];
    
    index(req, res) {
        res.send("Hello World!");
    }
    }
    
    server.start(() => console.log("Server started"));
    server.loadGlobalMiddleWares([
    (req, res, next) => {
        console.log("Middleware 1");
        next();
    },
    ]);
    server.loadControllers([new IndexController()]);
    
  2. Controller Level Middleware
    This middleware are invoked only on the routes that are defined in the controller.

    for example: if we have a Controller with path /user/. We need to use a function in every request that is defined in the controller.

    const { Server, Controller, Methods } = require("express-oops");
    const express = require("express");
    
    const server = new Server(express(), 3000);
    
    class IndexController extends Controller {
    path = "";
    routerMiddleWares = [
        [
        (req, res, next) => {
            console.log("Middleware 1");
            next();
        },
        ]
    ];
    
    routes = [
        {
        method: Methods.GET,
        path: "/",
        handler: this.index,
        },
    ];
    
    index(req, res) {
        res.send("Hello World!");
    }
    }
    
    server.start(() => console.log("Server started"));
    server.loadControllers([new IndexController()]);
    
  3. Local Middlewares
    Middlewares are defined in the route.

    const { Server, Controller, Methods } = require("express-oops");
    const express = require("express");
    
    const server = new Server(express(), 3000);
    
    class IndexController extends Controller {
    path = "";
    routerMiddleWares = [];
    
    routes = [
        {
        method: Methods.GET,
        path: "/",
        handler: this.index,
        localMiddleWares: [
            (req, res, next) => {
            console.log("Middleware 1");
            next();
            },
        ],
        },
    ];
    
    index(req, res) {
        res.send("Hello World!");
    }
    }
    
    server.start(() => console.log("Server started"));
    server.loadControllers([new IndexController()]);
    

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay