Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var_dump (PHP) equivalent for node.js

I'm looking for an equivalent of var_dump in node.js.

I'd like to send the content of the request / response circular 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.

like image 841
Emil A. Avatar asked Nov 17 '13 18:11

Emil A.


People also ask

Is there a Var_dump in JavaScript?

The var_dump equivalent in JavaScript? Simply, there isn't one. Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM tab.

What does Var_dump mean in PHP?

The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.

What does Var_dump return?

@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .


3 Answers

To get what you’d get in the console by using console.log as a string for sending to the client as part of your response, you can use 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"); 
like image 157
Ry- Avatar answered Sep 20 '22 17:09

Ry-


You can simply use the NPM package 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/var_dump

Thank me later!

like image 35
ankurnarkhede Avatar answered Sep 19 '22 17:09

ankurnarkhede


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

like image 30
Jeff Tian Avatar answered Sep 18 '22 17:09

Jeff Tian