<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Muhammad Semeer</title>
    <description>The latest articles on Forem by Muhammad Semeer (@muhammadsemeer).</description>
    <link>https://forem.com/muhammadsemeer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F728876%2F1b36ba44-49de-48de-a54b-e809f5b0b014.jpeg</url>
      <title>Forem: Muhammad Semeer</title>
      <link>https://forem.com/muhammadsemeer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/muhammadsemeer"/>
    <language>en</language>
    <item>
      <title>Write Express Server in Class Based or Object Oriented Way.</title>
      <dc:creator>Muhammad Semeer</dc:creator>
      <pubDate>Wed, 13 Apr 2022 13:57:01 +0000</pubDate>
      <link>https://forem.com/muhammadsemeer/write-express-server-in-class-based-or-object-oriented-way-kpn</link>
      <guid>https://forem.com/muhammadsemeer/write-express-server-in-class-based-or-object-oriented-way-kpn</guid>
      <description>&lt;p&gt;We have seen lot's of examples of express server function based approaches. So I just created a simple express server with OOPs. &lt;a href="https://www.npmjs.com/package/express-oops"&gt;Checkout&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now We can use express in Class Based Approach
&lt;/h2&gt;

&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express-oops
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also make sure that you have Node.js 14 or newer in order to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create A Server
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Server } = require('express-oops');

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

const server = new Server(app, 3000);

server.start();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start a server on port 3000.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create A Controller
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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(() =&amp;gt; console.log("Server started"));
server.loadControllers([new IndexController()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add Middlewares
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Global Middlewares&lt;br&gt;
This middleware are invoked on every request.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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(() =&amp;gt; console.log("Server started"));
server.loadGlobalMiddleWares([
(req, res, next) =&amp;gt; {
    console.log("Middleware 1");
    next();
},
]);
server.loadControllers([new IndexController()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Controller Level Middleware&lt;br&gt;
This middleware are invoked only on the routes that are defined in the controller.&lt;/p&gt;

&lt;p&gt;for example: if we have a Controller with path &lt;code&gt;/user/&lt;/code&gt;. We need to use a function in every request that is defined in the controller.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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) =&amp;gt; {
        console.log("Middleware 1");
        next();
    },
    ]
];

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

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

server.start(() =&amp;gt; console.log("Server started"));
server.loadControllers([new IndexController()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Local Middlewares&lt;br&gt;
Middlewares are defined in the route.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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) =&amp;gt; {
        console.log("Middleware 1");
        next();
        },
    ],
    },
];

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

server.start(() =&amp;gt; console.log("Server started"));
server.loadControllers([new IndexController()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>express</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
    </item>
  </channel>
</rss>
