-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Neville Mehta
authored and
Neville Mehta
committed
Jul 22, 2024
1 parent
5c07a8b
commit fdc526b
Showing
3 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/integrations/node/test/fixtures/node-middleware/src/node-middleware.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const combineMiddleware = (...middlewares) => { | ||
return (req, res, next) => { | ||
let index = 0; | ||
|
||
const run = (err) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
if (index >= middlewares.length) { | ||
return next(); | ||
} | ||
const middleware = middlewares[index++]; | ||
try { | ||
middleware(req, res, run); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
run(); | ||
}; | ||
}; | ||
|
||
const middleware_one = (req, res, next) => { | ||
if (req.url === '/middleware-one') { | ||
res.end('This is middleware one'); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
const middleware_two = (req, res, next) => { | ||
if (req.url === '/middleware-two') { | ||
res.end('This is middleware two'); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
export default combineMiddleware(middleware_one, middleware_two); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters