elysiajs / elysia-cors

Plugin for Elysia that for Cross Origin Requests (CORs)
MIT License
31 stars 9 forks source link

{"name":"TypeError","message":"Response constructor: Invalid response status code 204"} is thrown when browser is making a OPTIONS preflight request to an Elysia server running on Node.js #47

Closed dtinth closed 8 months ago

dtinth commented 8 months ago

Node.js’s Response constructor throws an error when response body is not null (e.g. empty string):

new Response('', { status: 204 })
/*
Uncaught TypeError: Response constructor: Invalid response status code 204
    at webidl.errors.exception (node:internal/deps/undici/undici:1636:14)
    at initializeResponse (node:internal/deps/undici/undici:5754:31)
    at new Response (node:internal/deps/undici/undici:5548:9)
*/

This is what @elysiajs/cors does:

Solution is to set body to null instead of ''.

Dependency versions

    "@elysiajs/cors": "1.0.0-beta.0",
    "elysia": "1.0.0-beta.1",
SaltyAom commented 8 months ago

c069ff9 set response to null as suggested, published on 1.0.0-beta.1

Let me know if this fix the problem.

dtinth commented 8 months ago

My tests are passing now, thank you!