Outputs variables in a visual, easy to read format based on Adobe ColdFusion's CFDump tag. Think of it as console.log on steroids.
Outputs variables in a visual, easy to read format based on Adobe ColdFusion's
CFDump tag with enhancements unique to node.js such as the syntax highlighting of functions. Think of it as
console.log on steroids.
nodedump works both server and client side.
For example, simply doing
nodedump(user) gives us:
The above is a dump of the variable
user created like so:
// validate username and passwordif!validateusername passwordreturn false;elseupdateSession;// user is signedInthissignedIn = true;return true;;var user =firstName: 'Charles'lastName: 'Teague'age: 21signedIn: falsesignIn: signInprojects:name: 'Allaire Spectra'status: 'Horrible death'name: 'ColdFusion 4.5'status: 'Been there done that';
console.log(user) we get:
firstName: 'Charles'lastName: 'Teague'age: 21signedIn: falsesignIn: Functionprojects:name: 'Allaire Spectra' status: 'Horrible death'name: 'ColdFusion 4.5' status: 'Been there done that'
Which is the typical output we have to rely on usually to do our debugging. As our variables become more complicated this becomes a painful way to know what's going on within them.
nodedump is based on the
CFDump tag of Adobe's ColdFusion which has long been a unique feature allowing developers to understand what's in any variable. Once you get accustomed to the color coding and layout of dumped output, your brain will be able to quickly see and understand what's in any variable you dump with just a glance. Pretty much all the options available for
CFDump have been included in nodedump.
Run this from your bash or command line:
$ [sudo] npm install nodedump
This will create a global function called
nodedump. Then in your view or wherever you output to the browser, whenever you want to dump the contents of a variable do:
See the calling nodedump section section for the various ways to call and name the function that is used for dumping.
To use nodedump client-side simply include the
nodedump-client.js (available in the nodedump-client directory) file in your page and then dump any variable you'd like. For instance:
nodedump client side example
The following example sets up a server, creates a test object and dumps it to the browser. Try it!
var http = require'http';nodedump = require'nodedump'dump;// validate username and passwordif!validateusername passwordreturn false;elseupdateSession;// user is signedInthissignedIn = true;return true;;var server = httpcreateServerconsole.log'Request received';console.log'url:'requesturl;// skip requests for faviconif requesturlindexOf'favicon.ico' > -1console.log'favicon requested';responsewriteHead500;responseend;console.log'Request ended';return;// start output to the browserresponsewriteHead200 "Content-Type": "text/html";var user =firstName: 'Charles'lastName: 'Teague'age: 21signedIn: falsesignIn: signInprojects:name: 'Allaire Spectra'status: 'Horrible death'name: 'ColdFusion 4.5'status: 'Been there done that';//capture dumpvar output = nodedumpuser;// write response to the browserresponsewrite'<html>'+ '<head>'+ '<title>nodedump example!</title>'+ '</head>'+'<body>'+output+'</body>'+'</html>';responseend;console.log'Request ended';//console.log('global.bnodedumpinited',global.bnodedumpinited);listen3000;console.log"Server has started.";
Options can be passed as an object whenever you
nodedump a variable as the second parameter, e.g.
The available options are:
label- String. Output on the header of the dump.
expand- Boolean/Array. Defaults to
true. The dump can be collapsed entirely by passing
false. Simply click on the headers in order to expand them. An array of complex types can be passed and those types of objects will be expanded while everything else will remain collapsed. For e.g.
['Array', 'Object', 'Function']
collapse- Boolean/Array. Defaults to
false. Works opposite the way
expanddoes. The dump can be collapsed entirely by passing
true. An array of types can be passed and those types of objects will be collapsed wherever they are in the dump. For e.g.
['Array', 'Object', 'Function']
show- Array. A list of object keys / array positions to show. Others not in the list will be hidden.
hide- Array. A list of object keys / array positions to hide.
hideTypes- Array. A list of complex types of objects to exclude from being output. e.g.
top- Number. The number of array positions of the dump variable to show. For objects, this is the number of keys of the top level to show.
levels- Number. How many nested levels of an object to dump down to.
sortKeys- Boolean. Defaults to
true. Tells nodedump to output the keys of objects sorted alphabetically. If
false, keys will be output in whatever order node.js returns them (usually the order in which they were added).
syntaxHighlight- Boolean. Defaults to
true. Tells whether or not the dump of functions should be syntax highlighted (color-coded).
dumpFunctionName- String. Defaults to
'nodedump'. Name to use for the nodedump function. E.g. if this is changed to
'dump'then in addition to doing
nodedump(vartodump)you can do
nodedumpuser1 expand: false label: 'User1';nodedumpuser2 expand: false label: 'User 2';
Clicking on the header of collapsed sections will expand them.
Notice that though the object has 6 keys, only the top 4 were output.
Notice that in the projects sub-array that the 3rd level is not shown.
Default options can be overriden by calling the
init method on nodedump. E.g.
require'nodedump'initdumpFunctionName: 'dump'top: 100sortKeys: falseexpand: false;
The above would set the default for all nodedumps as follows:
nodedump(vartodump). Keep in mind that the latter would still work.
require('nodedump') will create the global function
nodedump that can be used to dump variables. As you've seen with the dumpFunctionName option you can add another name for the function and use that instead. Another way to do this is to set your variable directly to the
dump function. E.g.:
d = require'nodedump'dump;
So now you'd be able to dump variables using
You can do this even when initializing nodedump with default options:
d = require'nodedump'init expand: false dump;