javascript 相当于 node.js 的 var_dump (PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20034283/
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-10-27 17:32:53  来源:igfitidea点击:

var_dump (PHP) equivalent for node.js

javascriptnode.js

提问by Emil A.

I'm looking for an equivalent of var_dumpin node.js.

我正在寻找等效的var_dumpin node.js

I'd like to send the content of the request/ responsecircular structure as a part of the response.

我想将请求/响应循环结构的内容作为响应的一部分发送。

I know I can see it in the console, but that's not what I'm looking for.

我知道我可以在控制台中看到它,但这不是我要找的。

回答by Ry-

To get what you'd get in the console by using console.logas a string for sending to the client as part of your response, you can use util.inspect.

要通过使用console.log作为响应的一部分发送到客户端的字符串来获取在控制台中获得的内容,您可以使用util.inspect.

"use strict";

const http = require("http");
const util = require("util");

http.createServer((request, response) => {
    response.setHeader("Content-Type", "text/plain;charset=utf-8");
    response.end(util.inspect(request));
}).listen(8000, "::1");

回答by ankurnarkhede

You can simply use the NPM package var_dump

你可以简单地使用 NPM 包 var_dump

npm install var_dump --save-dev

Usage:

用法:

const var_dump = require('var_dump')

var variable = {
  'data': {
    'users': {
      'id': 12,
      'friends': [{
        'id': 1,
        'name': 'John Doe'
      }]
    }
  }
}

// print the variable using var_dump
var_dump(variable)

This will print:

这将打印:

object(1) {
    ["data"] => object(1) {
        ["users"] => object(2) {
            ["id"] => number(12)
            ["friends"] => array(1) {
                [0] => object(2) {
                    ["id"] => number(1)
                    ["name"] => string(8) "John Doe"
                }
            }
        }
    }
}

Link: https://www.npmjs.com/package/@smartankur4u/vardump

链接:https: //www.npmjs.com/package/@smartankur4u/vardump

Thank me later!

晚点再谢我!

回答by Jeff Tian

There is a npm package called circular-json, it's very good to use. I wonder why it is not built-in...

有一个 npm 包叫circular-json,很好用。我想知道为什么它不是内置的...