mirror of
https://github.com/sergeyyarkov/node-docker-example.git
synced 2026-10-11 02:09:22 +03:00
feat: added support for async middlewares
This commit is contained in:
parent
ac9248bf7f
commit
c00ec6779a
@ -2,12 +2,18 @@ import http from 'node:http';
|
||||
import Response from './response.js';
|
||||
|
||||
/**
|
||||
* This middleware just prints text to console
|
||||
* This middleware waits for a few milliseconds
|
||||
*
|
||||
* @param {http.ClientRequest} req
|
||||
* @param {Response} res
|
||||
* @param {function} next
|
||||
*/
|
||||
export const helloMiddleware = (req, res) => {
|
||||
console.log('[LOG]: Hello from `helloMiddleware` function');
|
||||
export const waitMiddleware = async (req, res) => {
|
||||
const wait = (ms = 1000) => {
|
||||
return new Promise((res) => {
|
||||
setTimeout(() => res(ms), ms);
|
||||
});
|
||||
};
|
||||
|
||||
await wait(50);
|
||||
};
|
||||
|
||||
@ -12,8 +12,8 @@ import {
|
||||
/**
|
||||
* Middlewares
|
||||
*/
|
||||
import { helloMiddleware } from '../middlewares.js';
|
||||
import { waitMiddleware } from '../middlewares.js';
|
||||
|
||||
router.get('/api/articles', getArticlesHandler).middleware([helloMiddleware]);
|
||||
router.get('/api/article/:id', getArticleHandler).middleware([helloMiddleware]);
|
||||
router.get('/api/articles', getArticlesHandler).middleware([waitMiddleware]);
|
||||
router.get('/api/articles/:id', getArticleHandler).middleware([waitMiddleware]);
|
||||
router.post('/api/article', createArticleHandler);
|
||||
|
||||
31
router.js
31
router.js
@ -1,7 +1,18 @@
|
||||
import http from 'node:http';
|
||||
import util from 'node:util';
|
||||
import { pathToRegexp } from 'path-to-regexp';
|
||||
import StringHelper from './helpers/string.js';
|
||||
|
||||
/**
|
||||
* Single route type
|
||||
* @typedef {{ method: string, handler: function, options: RouteOptions, middlewares: Array<function> }} Route
|
||||
*/
|
||||
|
||||
/**
|
||||
* Route options type
|
||||
* @typedef {{ params: Array, exact: boolean, regexp: RegExp }} RouteOptions
|
||||
*/
|
||||
|
||||
class Router {
|
||||
constructor() {
|
||||
/**
|
||||
@ -29,8 +40,7 @@ class Router {
|
||||
* Find exact route
|
||||
*/
|
||||
if (this.routes.hasOwnProperty(mask)) {
|
||||
this.#middlewares(route, req, res);
|
||||
route.handler(req, res);
|
||||
this.#middlewares(route, req, res).then(() => route.handler(req, res));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -49,8 +59,9 @@ class Router {
|
||||
req.params[param.name] = match[1];
|
||||
});
|
||||
}
|
||||
this.#middlewares(route, req, res);
|
||||
route.handler(req, res);
|
||||
this.#middlewares(route, req, res).then(() =>
|
||||
route.handler(req, res)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -62,20 +73,20 @@ class Router {
|
||||
/**
|
||||
* This function will execute every middleware of a route
|
||||
*
|
||||
* @param {*} route
|
||||
* @param {http.ClientRequest} req
|
||||
* @param {Response} res
|
||||
* @param {Route} route Route
|
||||
* @param {http.ClientRequest} req HTTP Request
|
||||
* @param {Response} res HTTP Response
|
||||
* @returns
|
||||
*/
|
||||
#middlewares(route, req, res) {
|
||||
if (!route || !route.middlewares || !Array.isArray(route.middleware)) {
|
||||
async #middlewares(route, req, res) {
|
||||
if (!route || !route.middlewares || !Array.isArray(route.middlewares)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlewares = route.middlewares.flat();
|
||||
if (middlewares.length !== 0) {
|
||||
for (const middleware of middlewares) {
|
||||
middleware(req, res);
|
||||
await middleware(req, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user