Your problem is divided into several parts, which I will try to address:
How to get requests on a server based on Node.JS?
Let's assume that you are using expressjs to get requests to the address /datos/
of your server application. Your code acts as endpoint
. To receive a request you must send it from a client. We will also assume that you are running it locally and that there is no defined path nor have you changed the default port of expressjs. Therefore, the endpoint
that you have defined for that function would be http://localhsot:3000/datos/
.
The function you use, get
, defines the http requesting method to be used to reach this function or this endpoint
. As it is a GET, accessing this url from a browser would get in the "Hello" terminal, since your function is executed. To the function you are using, you pass another one that you define, in the form of an arrow function, with two input parameters, req
and res
. These parameters represent the req uest and the res ponse (in Spanish, request and response). In the parameter req
you will therefore have all the information about the request that has been made, and in the parameter res
you can set all the information about the answer you want to give to the request. In your case, the browser would be "hung", since you do not send any response to this request. The normal thing would be that you gave him an answer, at least to say that everything went well (or with the error that has occurred otherwise). Therefore, the minimum correct would be to put res.sendStatus(200)
at the end of your function (equivalent to res.status(200).send('OK')
).
How to send requests from a client to this server?
Focusing only on the GET method, one way to do this is by simply accessing from the browser as we said before. Another is, as you propose, launching a request from a javascript code executed on the client. Again, there are many ways to do this from javascript, but since you have not provided us with more information, I'm going to assume that you use javascript vanilla without any framework, without jQuery or anything. To make a request from javascript you can use the class XMLHttpRequest
as explained in this question from SO :
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
This proposed function has two parameters, one for the URL that you want to reach, and another to provide a function with what to do when receiving the server response. In your case, if you want to ignore the answer and simply send the request, we could simplify the function to something like this:
function httpGetAsync(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
How to send parameters in the request?
For a request for a GET method, the parameters go in the same URL, so it's as simple as concatenating the variable you want to send with the appropriate format. Returning to the case we are developing, the url you want to send is http://localhsot:3000/datos/?dato2=mundo
. The interrogation indicates that the parameters begin, with the name of the parameter first, a% sign =
and the value of the parameter. To add more parameters, each new parameter must be separated with the sign &
. Therefore, using the previous function:
var dato2 = 'mundo';
var endpoint = 'http://localhost:3000/datos/';
var theUrl = endpoint + '?dato2=' + dato2;
httpGetAsync(theUrl);
How to process these parameters on the server?
Now we have the parameter on our server. It will be found, as we named in the first point, within the variable req
. Since we have sent the parameter in the url itself, we can find it within the object query
of req
, so console.log(dato1, req.query.dato2);
would give you as a result Holamundo
.
There are other ways to do it. For example, as you propose in a comment to your answer @shadow , you can use part of the url as an endpoint parameter instead of sending it as a parameter of the request. In that case, the url to form would be directly http://localhost:3000/datos/mundo
, the url of your function get '/datos/:dato2'
and the way to access the parameter would be through the params object instead of the query object, console.log(dato1, req.params.dato2);
I hope this answer helps you understand a bit better how the system you are building works in a basic way.