Javascript 从 NodeJS/Express 发送 JSON 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45128956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:55:31  来源:igfitidea点击:

Sending JSON response from NodeJS/Express

javascriptjsonnode.jsexpress

提问by Dreemlife

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

抱歉 n00b 问题我有点卡住了所以我希望你们能把我放在正确的方向上。

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

我正在制作一个通过 NODEJS 从 REST API 检索数据的应用程序。(这是成功且有效的)。

I then have a listen URL (my own API) in express that I invoke by going to the browser http://localhost/apior 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.

然后我在 express 中有一个侦听 URL(我自己的 API),我通过转到浏览器http://localhost/api或使用 POSTMAN 来调用它。到目前为止一切顺利,我在控制台(节点控制台)中看到,当我看到 JSON 响应时,我的请求得到了完美的处理,但是,我还希望在浏览器或 POSTMAN 中将 JSON 响应视为 JSON 响应,而不仅仅是控制台 我知道我的(简单)代码中遗漏了一些东西,但我才刚刚开始......请帮助我,这是我的代码。

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!

非常感激!

采纳答案by Dreemlife

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

非常感谢 ProgXx,原来我使用了相同的 res 和响应名称。这是最终的代码。非常感谢 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;

回答by Anurag Singh Bisht

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

要将 json 响应从 express 服务器发送到前端,请使用res.json(request.json)而不是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      
});