最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending JSON response from NodeJSExpress - Stack Overflow

programmeradmin1浏览0评论

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: '',
    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!

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!

Share Improve this question asked Jul 16, 2017 at 13:08 DreemlifeDreemlife 1211 gold badge1 silver badge6 bronze badges 3
  • what should request.json be? – Jonas Wilms Commented Jul 16, 2017 at 13:12
  • No request in particular ;-) I just hit the /api url without any request parameters. I wanted to send the response from my request (the API i consume) via res.send(request.json) but that does not return me the body of the request that was what i was trying to figure out how to do. So in short. My request to the JSON API works and I want that request outputted as soon as I hit my own API via localhost:3000/api -> invoke request to other api -> 1) retrieve data, 2) console log that data BUT 3) also show it in browser as JSON. 3 does not seem to work, 1 and 2 work – Dreemlife Commented Jul 16, 2017 at 13:14
  • maybe res.json(json) ?? – Jonas Wilms Commented Jul 16, 2017 at 13:16
Add a comment  | 

2 Answers 2

Reset to default 7

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      
});

Much thanks to ProgXx, turned out I used the same res and response names. Here is the final code. Much thanks ProgXx

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',
        'User-Agent': 'my-reddit-client'
    }
};

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

});

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

module.exports = app;
发布评论

评论列表(0)

  1. 暂无评论