Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending JSON response from NodeJS/Express

sorry for the n00b question I have been kinda stuck so I was hoping you guys could put me in the right direction.

I am making an app that is retrieving data by NODEJS from a REST API. (This is a success and works).

I then have a listen URL (my own API) in express that I invoke by going to the browser http://localhost/api or by using POSTMAN. So far so good, I see in the console (NODE Console) that my request gets handled perfectly as I see the JSON response, however, I would also like to see the JSON response in the browser or POSTMAN as JSON Response, not just the console I know I am missing something in my (simple) code but I am just starting out.... Please help me out here is my code.

var express = require("express"); 
var app = express();
const request = require('request');

const options = {  
    url: 'https://jsonplaceholder.typicode.com/posts',
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Accept-Charset': 'utf-8',
    }
};

app.get("/api", function(req, res)  { 
    request(options, function(err, res, body) {  
    var json = JSON.parse(body);
    console.log(json);
    });
    res.send(request.json)
    });

app.listen(3000, function() {  
    console.log("My API is running...");
});

module.exports = app;

Much appreciated!

like image 526
Dreemlife Avatar asked Jul 16 '17 13:07

Dreemlife


People also ask

How do I return a JSON response in node JS?

Go to http://localhost:3000/multiple, you will see the following output. Go to http://localhost:3000/array, you will see the following output. Method 2 (Using HTTP interface): Although the first method is sufficient for most solutions, there is another method that uses HTTP interface by Node. js and returns JSON data.

What is response JSON in Express?

Sep 13, 2019. Express response objects have a json() function. The res. json() function takes a single parameter, an object obj , serializes it to JSON, and sends it in the HTTP response body.

How do I send a response using Express?

How to send a response back to the client using Express. In the Hello World example we used the Response. send() method to send a simple string as a response, and to close the connection: (req, res) => res.


1 Answers

To send json response from express server to the frontend use res.json(request.json) instead of res.send(request.json).

app.get("/api", function(req, res)  { 
  request(options, function(err, res, body) {  
    var json = JSON.parse(body);
    console.log(json); // Logging the output within the request function
  }); //closing the request function
  res.send(request.json) //then returning the response.. The request.json is empty over here
});

Try doing this

app.get("/api", function(req, res)  { 
  request(options, function(err, response, body) {  
    var json = JSON.parse(body);
    console.log(json); // Logging the output within the request function
    res.json(request.json) //then returning the response.. The request.json is empty over here
  }); //closing the request function      
});
like image 94
Anurag Singh Bisht Avatar answered Sep 22 '22 03:09

Anurag Singh Bisht