Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on repl module in nodejs

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 420
    Comment on it

    Welcome to FindNerd. Today we are going to discuss repl module in nodejs. repl module deals in Real Eval Print Loop implementation that can work as standalone program or can be work with other applications. Nodejs includes repl as a core module. You do not need to install it in your application. You can simply load it by using below command.

     

    var repl  = require('repl');

     

    Repl module provides you repl.REPLServer Class. When you pass the input, the instance of repl.REPLServer class will accept it and work with input as per the user defined function. In the end, pass the output as result.

     

     

    Default behavior

    If you open the terminal and type the node in command prompt then it will provide you the space to write. You can perform mathematical operations and create the variables or functions as well. Please have a look.

     

    If you check above screenshot then you will get a clear idea. In above image we typed node and we got a space to write. After that, we defined a variable on and pressed enter then we got undefined. We did the same again, created an another variable that is findnerd and its value is 12. We performed two operations. First, we performed the multiplication and we got 12 because we assigned value 1 to evon and 12 to findnerd. After that, we performed operation addition and we got 13.

    We created a function named master and passed one argument to the function. we are printing a message to the console with a number. You can see in the image we called the function without an argument then it attached the undefined in the message and returned the undefined in the next line. One question should be raised here why we are getting undefined so the answer is, we did not return nothing in the function that's why we got undefined. Kindly check below image in which we are returning number.

     

    // repl.js
    var http = require('http');
    var repl = require('repl');
    var trains = {
    	"13": {
    		"number": 13,
    		"origin": "MKE",
    		"destination": "LAX",
    		"departs": "10:00 AM",
    		"arrives": "12:00 PM"
    	},
    	"18": {
    		"number": 18,
    		"origin": "LAX",
    		"destination": "PHX",
    		"departs": "9:00 AM",
    		"arrives": "9:30 AM"
    	}
    	};
    	var handlerequest = function(req,res){
    	res.writeHead(200,{"Content-Type":"text/plain"});
    	res.end('You are Nerd');
    }
    var server = http.createServer(handlerequest);
    server.listen(3000,'localhost');
    
    var prompt = repl.start({prompt: 'train> '});
    prompt.context.data = trains;

     

    In above example, we have loaded the HTTP and repl modules and created an object of train details. We have started the server using createServer and listen functions. We have started the prompt using start function and created variable data using context property. Run the below command to start the application.

    node repl.js

    Context properties are not read only. You have to use the defineProperty function to make it read only global. Please have a look.

    Object.defineProperty(prompt, 'data', {
      configurable: false,
      enumerable: true,
      value: trains
    });

     

    Output in the terminal for repl based example.

     

    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: