Git Product home page Git Product logo

Comments (2)

cmorten avatar cmorten commented on June 2, 2024

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.

cmorten avatar cmorten commented on June 2, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.