Express.js — это промежуточное ПО.
«Ссылка:» согласно wikipedia.org
Промежуточное ПО – это компьютерное программное обеспечение, предоставляющее программные приложения помимо тех, что доступны в операционной системе, называемое "программным клеем".
ПО промежуточного слоя — это расплывчатый термин, который часто используется в CS; Центральная концепция состоит в том, чтобы позволить взаимодействовать двум различным типам программного обеспечения.
Мы можем использовать промежуточное ПО в Express, используя ключевое слово use вот так.
//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 need app.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
Как это работает
Теперь, если мы хотим отправить клиенту что-то обратно после того, как он сделал запрос, мы можем использовать ключевое слово send
// 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