Skip to content

Commit 77df993

Browse files
committed
Small improvements to server.ts
- use correct content-type in the rewriter response - use 500 server error for errors
1 parent 7a40545 commit 77df993

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

scripts/server.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,34 @@ const server = Bun.serve({
4343
}
4444

4545
const filepath = './' + path.join('/');
46-
4746
try {
4847
const file = Bun.file(filepath);
4948
if (await file.exists()) {
50-
console.log(styleText('greenBright', `200: Found → '${filepath}'`));
51-
52-
if (/html$/.test(filepath)) {
49+
console.log(
50+
styleText('greenBright', `200: Found → '${file.name}'`) +
51+
styleText('green', ` ${file.type}'`)
52+
);
53+
if (/(html|javascript)/.test(file.type)) {
5354
const content: string = await file.text();
54-
return new Response(replaceCDNPath(content), { headers: { 'content-type': 'text/html' }});
55+
return new Response(replaceCDNPath(content), { headers: { 'content-type': file.type }});
5556
} else {
5657
return new Response(file);
5758
}
59+
} else {
60+
console.log(styleText('redBright', `404: Not Found → '${filepath}'`));
61+
return new Response('Not Found', { status: 404 });
5862
}
5963
} catch (error) {
60-
// Handle potential errors during file access
61-
console.error(`Error serving file: ${filepath}`, error);
64+
console.log(styleText('redBright', `500: Server Error → '${filepath}'`));
65+
console.error(error);
66+
return new Response('Internal Server Error', { status: 500 });
6267
}
63-
64-
// If file not found or error, return 404
65-
console.log(styleText('redBright', `404: Not Found → '${filepath}'`));
66-
return new Response('Not Found', { status: 404 });
6768
}
6869
});
6970

7071
console.log('');
7172
console.log(styleText(['blue', 'bold'], `Bun v${Bun.version}`));
72-
console.log(styleText('cyanBright', `Serving: ['docs/*', 'dist/*']`));
73+
console.log(styleText('cyanBright', `Serving: docs/*, dist/*`));
7374
console.log(styleText('cyanBright', `Listening: ${server.url}`));
7475
console.log(styleText(['whiteBright', 'bold'], `Ctrl-C to stop`));
7576
console.log('');

0 commit comments

Comments
 (0)