How to return json from the server node js?

Good day. The client sends the server a request, the server should return json by taking it from another server.'/hello', jsonParser, function(req, res){

 let url = "";

 let options = {json: true};

 request(url, options, (error, res, body) => {
 if (error) {
 return console.log(error)

 if (!error && res.statusCode == 200) {

 Res. send(body) //I abuse


The handler code from the browser:

var button = document.querySelector("button");
 button.addEventListener("click", function() {
 console.log("Button clicked.");
 const request = new XMLHttpRequest();
 const url = "my_api/hello"'POST', url);
 request.addEventListener("load", function () {
 let receivedJson = JSON.parse(request.response);
 console.log(receivedJson.Date, "-", receivedJson.Date); // watch the server's response

April 19th 20 at 12:24
1 answer
April 19th 20 at 12:26
Are you trying to invoke the method .send() the variable res higher scope.'/hello', jsonParser, function(req, res){
 // here the "res" one

 request(url, options, (error, res, body) => {
 // here "res" is another
 // so res.statusCode - a res.send() - no

I hope it is clear explained.
And how can you request to return the data to the router, so he returned them to the browser using res.json? - hadley95 commented on April 19th 20 at 12:29
@hadley95, to declare a variable, put data from request and will be available in the router - rusty_Schultz commented on April 19th 20 at 12:32
@hadley95, For good, to use async/await. Well, to do Promise. - Brai commented on April 19th 20 at 12:35

Find more questions by tags Node.js