Skip to content

Commit

Permalink
fix: status 204 fatal error
Browse files Browse the repository at this point in the history
  • Loading branch information
imzlh committed Feb 10, 2025
1 parent 8079c99 commit 686d5cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ const [ , scheme, hostname, _port ] = config.server.match(/^(\w+):\/\/(.+)\:(\d+
new Server(scheme, port, hostname)
.header('Access-Control-Allow-Origin', '*')
.header('Access-Control-Allow-Methods', 'GET,POST,PUT')
.header('Access-Control-Allow-Headers', 'Content-Type')
.on('GET', async res => {
const postid = res.url.searchParams.get('id');
if(!postid) return res.status(404).end();
const comments = await Promise.all(
(await Comment.get_by_postid(postid))
.map(comment => comment.export())
);
if(comments.length == 0) res.status(204).end();
res.end(encode(comments));
})
.on('POST', async res => {
Expand Down
7 changes: 6 additions & 1 deletion server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class vResponse{
end(chunk?: string | Uint8Array | ReadableStream<Uint8Array>){
if(chunk && !(chunk instanceof ReadableStream)) this.chunk(chunk);
const body = chunk instanceof ReadableStream? chunk : new Blob(this.$content),
res = new Response(body, {
res = new Response(this.$status == 204 ? null : body, {
status: this.$status,
headers: this.$headers
});
Expand Down Expand Up @@ -87,6 +87,11 @@ export default class Server{

start(){
const handler = (req: Request, inf: Deno.ServeHandlerInfo) => {
if(req.method == "OPTIONS")
return new Response(null, {
status: 204,
headers: new Headers(this.$defHeader)
});
// deno-lint-ignore no-async-promise-executor
return new Promise<Response>(async rs => {
const res = new vResponse(req, inf.remoteAddr, new Headers(this.$defHeader), rs);
Expand Down

0 comments on commit 686d5cd

Please sign in to comment.