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

can jump 404 when that page does not exist #5701

Merged
merged 19 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions 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"
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@astrojs/webapi": "^1.1.1",
Expand All @@ -43,6 +43,7 @@
"astro-scripts": "workspace:*",
"chai": "^4.3.6",
"mocha": "^9.2.2",
"node-mocks-http": "^1.11.0"
"node-mocks-http": "^1.11.0",
"cheerio": "^1.0.0-rc.11"
}
}
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('....')
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
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.