Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between a Controller and a Middleware

I am writing and API in express.js. the original API I wrote only utilized routes and raw SQL queries. I have since rewritten the API for the most part NOW using an ORM to react models and migrations.

My question what is the difference and use cases for middleware and controllers. currently only using middleware because most sources online online only explain what a middleware is.

I don't understand the use case of a controller. and I don't want to omit it from my API if its used in proper programming conventions

like image 373
delavago1999 Avatar asked Jul 30 '19 14:07

delavago1999


2 Answers

You should see middleware as a step in your API and controllers as the entity that will actually respond to the requests.

Bellow is an example where authenticationMiddleware is a middleware because it is a step during the processing but should not return the response. It can though, in case of error.

Then getItems actually handle the logic specific to this calls.

As a rule of thumb, middleware are often reused more than once and often they do not response. On contrary, controller respond and are most of the time specific to one endpoint.

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

function authenticationMiddleware(req, res, next) {
  // Check that the user is authenticated using req.headers.Authorization
  // for example

  if (authenticated) {
    // The user is authenticated, we can go to the next step
    next();

  } else {
    // The user is not authenticated, we stop here
    res.status(401);
    res.send("Error during authentication");
  }
}

function getItems(req, res, next) {
  // Here we focus on the actual response, we assume that the user is authenticated
  res.send({ items: [] });
}

app.get("/items", authenticationMiddleware, getItems);
app.post("/items", authenticationMiddleware, createItems); // Re-use the same middleware

app.listen(3000);
like image 168
Thom Avatar answered Sep 28 '22 03:09

Thom


If you're referring to the node/express terminology, middleware are simply the callbacks used by the routing functions/methods (get, set, delete, use, etc). Callbacks can either send or not send responses back to the client. The callbacks are really the 'controllers' if you'd like to call them that (eg: ASP.NET Core MVC) but that's up to you. Below is a quote from Express. Note the term 'middleware' and where there's no mention of a 'controller'.

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

'Express' also defines different types of middleware which is useful:

  • Application-level middleware
  • Router-level middleware
  • Error-handling middleware
  • Built-in middleware Third-party middleware

Here's another nice look at it from Mozilla's pov that does mention a few controller/callback examples.

Beyond that, you can define what a 'controller' is within your team and the naming convention follows from there. Key is your SOLID profile and how you separate your concerns.

like image 26
MoMo Avatar answered Sep 28 '22 03:09

MoMo