【问题标题】:How to send the express "req" object to client side [duplicate]如何将快递“req”对象发送到客户端[重复]
【发布时间】:2018-08-31 09:19:21
【问题描述】:

我不确定这是否可行,但我想在客户端查看 req 对象的全部内容

const express = require('express');

const app = express();

app.get('/', (req, res) => {
    // send req object to the client
    res.json(req);
});

app.listen(5000, () => {
    console.log('Server successfully started on port 5000');
});

这将导致如下错误:

TypeError:将循环结构转换为 JSON

【问题讨论】:

  • 您将无法发送整个对象

标签: json node.js express


【解决方案1】:

req 对象包含大量数据。我认为您不需要将所有这些都发送回客户端。

您应该选择要发回的内容,并确保这些值不会造成循环问题。


喜欢

res.json({
  body: req.body,
});

【讨论】:

  • 感谢您的回答。我只是更喜欢将其作为一种调试方法而不是其他方法,因为我更容易在客户端查看整个对象。
【解决方案2】:

发送由指定数据的字符串化版本组成的 JSON 响应。 用法:

return res.json([statusCode, ] data);

例子:

var info = [
{id:1, name: "test 1"},
{id:2, name: "test 2"}
]
const express = require('express');
const app = express();
app.get('/', (req, res) => {
// send req object to the client
res.json(info);
});
app.listen(5000, () => {
console.log('Server successfully started on port 5000');
});

或者我认为最好使用body-parser

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-27
    • 2015-10-27
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多