Skip to content

Commit

Permalink
fix(@astrojs/node): handler should work with express
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 21, 2023
1 parent 95120ef commit 785ca46
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-tigers-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Fix an issue where `express` couldn't use the `handler` in `middleware` mode.
3 changes: 2 additions & 1 deletion packages/integrations/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"cheerio": "1.0.0-rc.12",
"mocha": "^9.2.2",
"node-mocks-http": "^1.13.0",
"undici": "^5.22.1"
"undici": "^5.22.1",
"express": "^4.18.2"
}
}
16 changes: 12 additions & 4 deletions packages/integrations/node/src/nodeMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import type { ErrorHandlerParams, Options, RequestHandlerParams } from './types'

// Disable no-unused-vars to avoid breaking signature change
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function (app: NodeApp, _mode: Options['mode']) {
export default function (app: NodeApp, mode: Options['mode']) {
return async function (...args: RequestHandlerParams | ErrorHandlerParams) {
let error = null;
let [req, res, next, locals] = args as RequestHandlerParams;
let locals;
let [req, res, next] = args as RequestHandlerParams;
if (mode === 'middleware') {
let { [3]: _locals } = args;
locals = _locals;
}

if (args[0] instanceof Error) {
[error, req, res, next, locals] = args as ErrorHandlerParams;

[error, req, res, next] = args as ErrorHandlerParams;
if (mode === 'middleware') {
let { [4]: _locals } = args as ErrorHandlerParams;
locals = _locals;
}
if (error) {
if (next) {
return next(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export async function get() {
let number = Math.random();
return {
body: JSON.stringify({
number,
message: `Here's a random number: ${number}`,
}),
};
}
44 changes: 42 additions & 2 deletions packages/integrations/node/test/node-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import express from 'express';

/**
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
Expand All @@ -14,7 +15,7 @@ async function load() {
return mod;
}

describe('behavior from middleware', () => {
describe('behavior from middleware, standalone', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;
Expand All @@ -34,7 +35,7 @@ describe('behavior from middleware', () => {
});

after(async () => {
await server.stop();
await server.close();
await fixture.clean();
delete process.env.PRERENDER;
});
Expand All @@ -53,3 +54,42 @@ describe('behavior from middleware', () => {
});
});
});

describe('behavior from middleware, middleware', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;

before(async () => {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.PRERENDER = false;
fixture = await loadFixture({
root: './fixtures/node-middleware/',
output: 'server',
adapter: nodejs({ mode: 'middleware' }),
});
await fixture.build();
const { handler } = await load();
const app = express();
app.use(handler);
server = app.listen(4321);
});

after(async () => {
await server.close();
await fixture.clean();
delete process.env.PRERENDER;
});

it('when mode is standalone', async () => {
const res = await fetch(`http://localhost:4321/ssr`);

expect(res.status).to.equal(200);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
expect(body.text()).to.contain("Here's a random number");
});
});
Loading

0 comments on commit 785ca46

Please sign in to comment.