-
-
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.
[feat/standalonenode-withmiddleware] add ability to pass middlewares …
…when using standalone node
- Loading branch information
Neville Mehta
authored and
Neville Mehta
committed
Jul 11, 2024
1 parent
75d118b
commit 0d27ffe
Showing
6 changed files
with
140 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/node': minor | ||
--- | ||
|
||
Add param to add middlewares when using @astrojs/node in standalone mode |
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
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
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
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
|
||
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