Home > Software engineering >  How to response dynamic content in Nodejs without Express
How to response dynamic content in Nodejs without Express

Time:01-30

      let reqBody = "";
      req
        .on("data", (chunk) => {
          reqBody  = chunk.toString();
        })
        .on("end", () => {
        const body = new URLSearchParams(reqBody);
        res.end("ok");
        });

I'm trying to access the request's body using NodeJS without Express but for some reason, I could not run res.end("ok");.

CodePudding user response:

You need to call req.end() as well

check Node.js document https://nodejs.org/api/http.html

With http.request() one must always call req.end() to signify the end of the request - even if there is no data being written to the request body

 let reqBody = "";
  req
    .on("data", (chunk) => {
      reqBody  = chunk.toString();
    })
    .on("end", () => {
    const body = new URLSearchParams(reqBody);
    res.end("ok");
    });
   req.end();
  •  Tags:  
  • Related