Express is all about middleware.

Express.js is all about middleware

Cite:” according to wikipedia.org

Middleware — is computer software that provides software applications beyond those available from the operating system — described as “software glue.”

Middleware is a vague term that gets used a lot in CS; The central concept is to allow two distinct types of software to interact.

We can use middleware in Express by employing the use keyword like so.

//import http
const http = require('http');
// then will need to install express and import that as well
const express = require('express');
const app = express();// then we tell express to 'use' middle ware like so
app.use()
//we'll want to pass in the request object and response and a tird argument called next
// and we'll do that by calling a function inside the use() function.
app.use((req, res, next) => {});
//we can call app.use as many time as we needapp.use((req, res, next)=> {
console.log("first middleware")
});
app.use((req, res, next)=> {
console.log("second middleware")
});
// but what in the terminal all we se is
=> first middleware
//the reason for this being that we don't have a way of passing our request to the next middleware so intsert next
app.use((req, res, next) => {
console.log("first middleware")
next()
})
app.use((req, res, next)=> {
console.log("second middleware")
})
//and what we see in the terminal is this
=>first middleware
=>second middleware

How it works

Now, if we want to send the client something back after they have made a request, we can use the send keyword

//
app.use((req, res, next)=> {
req.send('<h1> some html </h1>')
// then we tell app to funnel to the next middleware
next()
})
//simple as that

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cameron J. Leverett

Cameron J. Leverett

26 Followers

I love tackling new and exciting challenges and working with software because it’s consistently changing, which is exciting.