0

I want to send some data or requests to my node server using the get method, and receive this data and manipulate it on the server side. I know there is some ways like using postman, but i want to send it from the web page. using post the data was successfully received. here is the code am using:

//server.js

var express = require('express');
var app = express();

app.listen(3000, () => console.log('server is running on port 3000'));

app.use(express.json({limit: '5mbs'}));
app.use(express.static('client'));
app.get('/', (req, res) => {console.log(req)});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>chat</title>
    </head>
<body>
    <h1>server is sending html</h1>
    <script src="index.js"></script>
</body>
</html>

index.js

const a = 5;
console.log(a);
options = {
  method: "get",
  headers: {"ContentType" : "application/json"},  
};
fetch('/', options)

as you can see am trying to send this data in a json format but in my code there is no data to send, but using post i was able at least to recive the request and i was able to console.logit and then parse the req.body. but with get there is no req.body , and am not able to even console.log req, and i am not having any error or some thing on the console. so what is wrong here, and how can i be able to send the constant a for example?

6
  • 1
    A GET request has no body sent with it. Data for a GET request has to be in the query parameters or headers. Significant data is usually sent with a POST request in the body of the request. Commented Sep 21, 2019 at 21:01
  • With GET it's not req.body, it's req.query Commented Sep 21, 2019 at 21:01
  • i can't receive req at all the console isn't showing it Commented Sep 21, 2019 at 21:16
  • @jfriend00 how am i supposed to send the data in the headers Commented Sep 21, 2019 at 21:34
  • What is the data for? How big is the data? What is the purpose of the request? Those answers determine how you should send it. Commented Sep 21, 2019 at 22:21

3 Answers 3

1

so looks like my code was missing two things first i wasn't sending any req because the fetch function was taking only url = '/' so nothing is send to the server from the client ('index.js'), and the solution to that is adding some thing to it that is the variable a for example.

fetch('/' + a);

and the second thing that needs to be done is on the server.js , '/:a' in the url parameter of the app.get function so it takes all whats on the right side of the '/' (and before an '/' on the right) and put it on the variable a

app.get('/:a', (req, res) => {console.log(req.params)});

req.params is an objet that is going to have this key: val pairs lets say we have an other value b that we want to send than:

fetch('/' + a + '/' + b)

and to receive it

app.get('/:a/:b', (req, res) => {console.log(req.params)});

this will print

{ a: '5', b: '6' }
Sign up to request clarification or add additional context in comments.

Comments

0

you can use QueryString method to send data to Server in URL; it's like this:

example.com/?id=232

id is QueryString and you can get this variable in Express by req.params.id

2 Comments

i am going to try it. can you explain why i cant see any thing in the console because am console.loging req and nothing is showed, so obviously req is not being send to the server.
and i think that the problem is not about the receiving i think it's about sending using the fetch() function
0

the POST method is used to send data to an endpoint while the GET method is used to request data from the endpoint.

If you need to send specific data to your endpoint use query parameters. The access it on your endpoint for your server side logic.

Refer to this article for better understanding

4 Comments

there is something wrong in my fetch function because i am not receiving any thing on my server, i think the url is not written well. any suggestions.
Great to see that you already got a solution, i hope that the article was helpful, cheers
thank you. i was just a little beginner in this stuff you know.
we are all beginners bro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.