var_dump (PHP) equivalent for node.js
There is a npm package called circular-json
, it's very good to use. I wonder why it is not built-in...
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!