Skip to content

Commit

Permalink
fix: Resolving Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
wuls committed Jan 10, 2023
2 parents 4a1cabf + 64b47aa commit ebfc104
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-snakes-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

can jump 404 when that page does not exist
2 changes: 1 addition & 1 deletion packages/integrations/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "mocha --exit --timeout 20000 test/"
"test": "mocha --exit --timeout 20000 test/node-middleware.test.js"
},
"dependencies": {
"@astrojs/webapi": "^1.1.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/integrations/node/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import type { NodeApp } from 'astro/app/node';
import type { IncomingMessage, ServerResponse } from 'http';
import type { Readable } from 'stream';
import { responseIterator } from './response-iterator';
import type { Options } from './types';

export default function (app: NodeApp) {
export default function (app: NodeApp, mode: Options['mode']) {
return async function (
req: IncomingMessage,
res: ServerResponse,
next?: (err?: unknown) => void
) {
try {
const route = app.match(req);

const route =
mode === 'standalone' ? app.match(req, { matchNotFound: true }) : app.match(req);
if (route) {
try {
const response = await app.render(req);
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ polyfill(globalThis, {
exclude: 'window document',
});

export function createExports(manifest: SSRManifest) {
export function createExports(manifest: SSRManifest, options: Options) {
const app = new NodeApp(manifest);
return {
handler: middleware(app),
handler: middleware(app, options.mode),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getResolvedHostForHttpServer(host: string | boolean) {
export default function startServer(app: NodeApp, options: Options) {
const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080;
const { client } = resolvePaths(options);
const handler = middleware(app);
const handler = middleware(app, options.mode);

// Allow to provide host value at runtime
const host = getResolvedHostForHttpServer(
Expand Down
10 changes: 7 additions & 3 deletions packages/integrations/node/test/api-route.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse, toPromise } from './test-utils.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';

describe('API routes', () => {
Expand All @@ -17,18 +17,22 @@ describe('API routes', () => {

it('Can get the request body', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');

console.log('....')
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/recipes',
});

handler(req, res);
req.send(JSON.stringify({ id: 2 }));

req.send(JSON.stringify({ id: 2 }));

let [buffer] = await done;

let json = JSON.parse(buffer.toString('utf-8'));

expect(json.length).to.equal(1);

expect(json[0].name).to.equal('Broccoli Soup');
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/node-middleware",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
---

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404</title>
</head>
<body><h1>404!!!!!!!!!!</h1></body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---

<html lang="en">
<head><title>node-middleware</title></head>
<style>
</style>
<body>
<div>1</div>
</body>
</html>
37 changes: 37 additions & 0 deletions packages/integrations/node/test/node-middleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import nodejs from '../dist/index.js';
import { loadFixture , createRequestAndResponse} from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe('test 404 cant load', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/node-middleware/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
});
describe('test 404', async () => {
let devPreview;

before(async () => {
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('when mode is standalone', async () => {
const res = await fixture.fetch('/error-page');

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

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

const h1 = $('h1');
expect(h1.text()).to.equal('404!!!!!!!!!!');
});
})
})
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebfc104

Please sign in to comment.