Comments (2)
Curious! The error is coming from the std/http lib module which Opine uses, but worth checking that not doing something strange with the body in Opineβ¦
Be curious to know if is reproduceable with just the std/http lib (outside of Opine).
from opine.
@creeperkafasi sorry took so long to look into this a bit more.
In this instance it is a case of the std lib's buffer read implementation not giving you back a particularly good error message π
the read(p: Uint8Array)
requires you to pass a Uint8Array
to which the stream can be read into. For example, we can fix up your example as follows:
import { opine, serveStatic } from "./mod.ts";
const port = 3000;
const hostname = "127.0.0.1";
const app = opine();
app
.use("/public", serveStatic(`${Deno.cwd()}/public`))
.post("/msg", async (req, res, _next) => {
// Determine the size of the message and therefore how big a buffer we need.
const contentLength = parseInt(req.headers.get("content-length") ?? "0");
const buffer = new Uint8Array(contentLength);
// Read the request body into our buffer
await req.body.read(buffer);
// Do something with that buffer - here we just respond with it to the POST request.
res.send(buffer);
});
app.listen({ port, hostname }, () => {
console.log(`http://${hostname}:${port}`);
});
If you want to look into the method a bit more, check out [he potential return types of the request body provided by the legacy http std lib module (REF: https://deno.land/[email protected]/http/server_legacy.ts#L91), and the core Deno docs on the Deno.Reader
interface which the http body satisifies (REF: https://doc.deno.land/builtin/stable#Deno.Reader).
I would also check out the Opine docs on req.body
(REF: https://github.com/asos-craigmorten/opine/blob/main/.github/API/request.md#reqbody) including some of the body parsing middleware that Opine provides, namely:
These can likely save you some time writing bespoke code for parsing and consuming the request body π
As this is an error with using the read()
method rather than a bug with Opine, I'm going to close - please reopen if disagree!
from opine.
Related Issues (20)
- Incompatible with Deno 1.14.0 HOT 4
- Dependency error, no exported member default for several dependencies HOT 1
- Cross-Platform Path Separatores in serveStatic HOT 1
- TS2305 ERROR in deps.ts with range-parser default export HOT 2
- Bug on deno std, or opine ??? HOT 4
- Outdated dependencies
- Some (but not all) static files fail to be served - ERR_CONTENT_LENGTH_MISMATCH HOT 11
- Getting a plain Deno Request object from an OpineRequest? HOT 8
- JSON middleware does not work on Deno Deploy HOT 1
- How to read a request received from a html form? HOT 2
- FormData parser middleware is missing HOT 1
- Deno.Buffer will be deprecated in Deno 2.0 HOT 1
- Type incompatibility with Deno 1.20.1 HOT 5
- Opine can't find template views on Deno Deploy HOT 3
- [QUESTION] how to send ReadableStream<Uint8Array>? HOT 2
- Requested module doesn't provide required data HOT 1
- How to upload file?? HOT 1
- Request protocol for WebSocket upgrade request is always http HOT 2
- server-sent-events with opine HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opine.