node.js url search/query portion is empty

user3368674

I am developing a website (client + server) that runs from the same machine. I haven't found any errors in chrome's developer tools, so I don't know where the problem is.

I try to POST a string from the client to the Node back-end, but upon performing

 url.parse(request.url, true).query

the result is empty.

Here is my client:

var xhttp = new XMLHttpRequest();

xhttp.open("POST", "http://localhost:8080/newComment", true);
xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhttp.send("val=" + commentString);

My commentString is "hey there"

Here is my server:

var path = url.parse(request.url).pathname;
else if (path == "/newComment") {

    console.log("query: " + url.parse(request.url, true).body);
    //setNewComment(comment);

    response.writeHead(200, {
        'Content-Type': 'text/plain', 
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
        }); // enable cors
    response.write("Recieved request");
    response.end();
}

And I am at the website

file:///C:/Users/.../website.html?name=hey+there

So I'd like my Node server to print out "query: hey there" but instead it prints "query undefined"

Efi Shtainer

You are trying to access the body, and this is right, yet the url doesn't hold the body's content, you should either accumulate it by listening to 'data' events:

let body = [];
request.on('data', (chunk) => {
            body.push(chunk);
           }).on('end', () => {
                 body = Buffer.concat(body).toString();
                 // at this point, `body` has the entire request body stored in it as a string
                               });

or use "express" and "body-parser".

1.Go to the request body section: https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hide the text input field portion in a form's GET query url when the value is an empty string

From Dev

node.js Buffer not empty

From Dev

node.js Buffer not empty

From Dev

Node js change url

From Dev

Javascript bookmarklet to remove a portion of url

From Dev

regex for identifying portion of a URL in javascript

From Dev

Get last portion of URL with PHP

From Dev

Using iron-router to go to a certain id section of the page (url#portion) in Meteor.js

From Dev

Shorthand for empty function in Node.js

From Dev

Creating an empty index in elasticsearch in node.js

From Dev

Empty Node.js/Express response

From Dev

Node js, Request body empty for certain websites

From Dev

Non empty object is not returned - Node.js

From Dev

Node js crypto sign returns empty string

From Dev

Creating an empty index in elasticsearch in node.js

From Dev

Node.js request.body is empty

From Dev

Node .JS Crawler to JSON output is empty

From Dev

ForEach returns empty array - Node.js

From Dev

Python-Shell / Node JS output empty

From Dev

Multer req.body empty in node js

From Dev

Validating a URL in Node.js

From Dev

Node JS Routing URL Conflict

From Dev

Detect URL at node js start

From Dev

What is the "jdbc:mysql:" portion of a database url referred to as?

From Dev

Trying to rewrite a portion of the url to a specific php file

From Dev

Accessing an value in defaultdict and stripping out url portion of it

From Dev

what is the best way to get a portion of this url

From Dev

Rewrite rule: change a portion of text in URL to another

From Dev

How to get a specific portion of the url using javascript?