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.
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.
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.
@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 .
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");
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!
There is a npm package called circular-json
, it's very good to use. I wonder why it is not built-in...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With