简体   繁体   中英

How to response dynamic content in Nodejs without Express

      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"); .

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM