JAVA SCRIPT - Interactively Trying Out Node Code Snippets with REPL - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Interactively Trying Out Node Code Snippets with REPL - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Saturday, March 9, 2019

JAVA SCRIPT - Interactively Trying Out Node Code Snippets with REPL

Interactively Trying Out Node Code Snippets with REPL


You can test JavaScript code snippets in jsFiddle or jsBin, but what about Node’s serverbased code snippets?


Use Node’s REPL (read-evalute-print-Loop), an interactive command-line version of Node that can run any code snippet. To use REPL, type node at the command line without specifying an application to run. If you wish, you can also specify a flag, like --harmony, to use the ECMAScript 6 functionality:

$ node --harmony

You can then specify JavaScript in a simplified emacs (sorry, no vi) line-editing style. You can import libraries, create functions—whatever you can do within a static appli‐ cation. The main difference is that each line of code is interpreted instantly:

> var f = function(name) {
... console.log('hello ' + name);
... }
> f('world');
hello world

When you’re finished, just exit the program:

> .exit


REPL can be started standalone or within another application if you want to set certain features. You type in the JavaScript as if you’re typing in the script in a text file. The main behavioral difference is you might see a result after typing in each line, such as the undefined that shows up in the runtime REPL. But you can import modules:

> var fs = require('fs');

And you can access the global objects, which we just did when we used require(). The undefined that shows after typing in some code is the return value for the execution of the previous line of code. Setting a new variable and creating a function are some of the JavaScript that returns undefined, which can get quickly annoying.

To eliminate this behavior, as well as make some other modifications, you can use the REPL.start() function within a small Node application that triggers REPL (but with the options you specify). The options you can use are:

• prompt: Changes the prompt that shows (default is >)

• input: Changes the input readable stream (default is process.stdin, which is the standard input)

• output: Changes the output writable stream (default is process.stdout, the stan‐ dard output)

• terminal: Set to true if the stream should be treated like a TTY, and have ANSI/ VT100 escape codes written

• eval: Function used to replace the asynchronous eval() function used to evaluate the JavaScript

• useColors: Set to true to set output colors for the writer function (default is based on the terminal’s default values)

• useGlobal: Set to true to use the global object, rather than running scripts in a separate context

• ignoreUndefined: Set to true to eliminate the undefined return values

• writer: The function that returns the formatted result from the evaluated code to the display (default is the util.inspect function) An example application that starts REPL with a new prompt, ignoring the undefined values, and using colors is:

var net = require("net"),
 repl = require("repl");
var options = {
 prompt: '-- ',
 useColors: true,
 ignoreUndefined: true,

Both the net and repl modules are necessary. The options we want are defined in the options object and then passed as parameter to repl.start(). When we run the ap‐ plication, REPL is started but we no longer have to deal with undefined values:

# node reciple11-5.js
-- var f = function (name) {
... console.log('hello ' + name);
... }
-- f('world');
hello world

As you can see, this is a much cleaner output without all those messy undefined print outs.

No comments:

Post a Comment

Post Top Ad