Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/standalonenode-withmiddleware] add ability to pass middleware to @astrojs/node when using standalone mode #11425

Conversation

nm123github
Copy link

@nm123github nm123github commented Jul 7, 2024

Add option to pass middleware to @astrojs/node when using standalone mode.

Changes

  • Make changes in the @astrojs/node to accept middleware file as a parameter
  • If the middleware param is passed, @astrojs/node (in standalone mode) will attempt to load the middleware's and run them.
  • The middleware file is expected to return the combined list of middleware's that need to run.
  • The standalone server will work as it works today, if no middleware option is passed.

Testing

I've tested this using a standalone app. Here are the changes:-

astro.config.mjs

import { defineConfig } from 'astro/config';
import node from '@astrojs/node';

// https://astro.build/config
export default defineConfig({
	output: 'server',
	adapter: node({
		mode: 'standalone',
		middleware: `${import.meta.dirname}/src/node_middleware.js`,
	}),
});

node_middleware.js

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);

Docs

Opened PR for updating docs in @astrojs/node integrations guide

withastro/docs#8789

Copy link

changeset-bot bot commented Jul 7, 2024

🦋 Changeset detected

Latest commit: 7653485

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Jul 7, 2024
@nm123github nm123github force-pushed the feat/standalonenode-withmiddleware branch 2 times, most recently from 9f17c6a to 0d27ffe Compare July 11, 2024 21:57
@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jul 17, 2024
@github-actions github-actions bot removed the pkg: astro Related to the core `astro` package (scope) label Jul 18, 2024
@nm123github nm123github force-pushed the feat/standalonenode-withmiddleware branch from ae1e106 to bc26e5b Compare July 18, 2024 08:26
@nm123github nm123github force-pushed the feat/standalonenode-withmiddleware branch 3 times, most recently from 920e583 to 0c70ad9 Compare July 19, 2024 06:59
@nm123github nm123github force-pushed the feat/standalonenode-withmiddleware branch from c12d275 to 7653485 Compare July 19, 2024 08:50
@ematipico ematipico marked this pull request as draft July 19, 2024 09:36
@nm123github nm123github force-pushed the feat/standalonenode-withmiddleware branch from cb884be to 7653485 Compare July 22, 2024 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant