You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order for Express to modernize it's APIs and support Web Standards in addition to Node built-ints it would be great to have an API that let's one use
the built-in Request and Response from the fetch API instead of Node's http.request and http.response
the build-in web streams ReadableStream and WritableStream instead of Node's steam.Readable and stream.Writable
How could this look?
Basic Example
As an idea Express could expose this in a non-breaking way by offering an alternative to the app.use() function.
// req is a `Request` the fetch API, a new `Response` is returnedapp.useWeb(async(req)=>{constreqBody=awaitreq.text();returnnewResponse(`<h1>Hi from Express<h1/><p>${req.body}</p>`,{headers: {"content-type": "text/html"}})});
Streaming Example
importfsfrom'node:fs/promises';constfile=awaitopen('./some/file/to/read');// req is a `Request` the fetch API, a new `Response` is returnedapp.useWeb(async(req)=>{constnodeStream=awaitfs.createWriteStream('./some-data.json');returnnewResponse(nodeStream.toWeb(),{headers: {"content-type": "application/json"}})});
If it it is difficult to mix and match the normal use with useWeb. Maybe and alternative approach could be to have a different API for creating the server.
const app = express.createAppWeb() // needs a better name
app.use(...) // this use function expects the signature from above
Compatibility
app.useWeb could serve as a compatibilty layer and normalize calls to use and useWeb under the hood. So under the hood express could continue to use node streams while it's possible to use web streams and fetch APIs as a user.
Benefits
NodeJS has adopted a lot of web standards in the past decade. It would be great for express to make it easy for users to use them.
Drawbacks
might increase the API surface of Express
some of the support NodeJS has for these APIs is still experimental. However, it seems like NodeJS is quite committed to supporting these new APIs
References
Deno has similar interface using Request/Response for Deno.serve
Open questions
how to deal with .next()?
how to deal with errors?
I realize supporting this might have bigger implications for the inner workings as well as API decisions of express, so this should be carefully considered
Thanks
Great to have Express 5 released ❤️
The text was updated successfully, but these errors were encountered:
Proposal
In order for Express to modernize it's APIs and support Web Standards in addition to Node built-ints it would be great to have an API that let's one use
Request
andResponse
from the fetch API instead of Node'shttp.request
andhttp.response
ReadableStream
andWritableStream
instead of Node'ssteam.Readable
andstream.Writable
How could this look?
Basic Example
As an idea Express could expose this in a non-breaking way by offering an alternative to the
app.use()
function.Streaming Example
In types
The
app.useWeb
function could have the signatureAlternative design
If it it is difficult to mix and match the normal
use
withuseWeb
. Maybe and alternative approach could be to have a different API for creating the server.Compatibility
app.useWeb
could serve as a compatibilty layer and normalize calls touse
anduseWeb
under the hood. So under the hood express could continue to use node streams while it's possible to use web streams and fetch APIs as a user.Benefits
NodeJS has adopted a lot of web standards in the past decade. It would be great for express to make it easy for users to use them.
Drawbacks
References
Deno
has similar interface usingRequest
/Response
forDeno.serve
Open questions
.next()
?I realize supporting this might have bigger implications for the inner workings as well as API decisions of express, so this should be carefully considered
Thanks
Great to have Express 5 released ❤️
The text was updated successfully, but these errors were encountered: