• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/10

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

10 Cards in this Set

  • Front
  • Back

What does REPL stand for?

Read, evaluate, print, loop

How do we access the REPL environment?

Type 'node' in the command line

What does require('foo') do?

Executes the javascript in a module named 'foo' and (usually) returns an object for us to use in the client module




Note that the parameter can be the name of a node package module or the path to a private module (one that isn't published on npm)

What else can 'require' be used for apart from loading module dependencies?





require can be used to execute "run once" code as well, such as database initialization, constructing log singletons, changing built-in types and objects, and any other single-run code

How are exports, require, module, __filename and __dirname made available in node?

Node automatically wraps JS files in a function as follows:




(function (exports, require, module, __filename, __dirname) { // The code we write will be here});


The exports etc params are passed in and made available to the JS code. Note this also stops our JS files polluting the global namespace.

If we have the following function in a JS file called math.js:




function add(x,y){


return x+y;


}




How would we create a module from this file and how would we consume the module from another module?

Use export.add = add in the math module and then require('./math') from a client module (assuming the client is in the same directory).




Note we're just using the path to the math module. If this was an npm module we'd just use it's name (as specified in package.json)

What happens if we call require('mmm') twice?

On the first call the JS in the module is executed and an object returned (if applicable). The object is then cached so that any subsequent calls will just retrieve the cached object.




One important caveat is that the cache key used is based on the resolved file name. So if a program requires mmm multiple times, but the required path is different (require("mmm") versus require('./othermodule/node_modules/mmm')), it’s enough to “break cache,” and this would be treated as loading a new module.

How do we create events in Node?

Use event emitters:




var events = require('events');


var EventEmitter = events.EventEmitter;


var emitter = new EventEmitter();




emitter.emit('start');


emitter.emit('count', 1);


emitter.emit('count', 2);




Here we create 3 event emitters. The first param is the name of the event and the second param is the event argument (if required)

How do we listen for events in Node?

To set up an event listener in Node, use the on(), addListener(), and once() methods. The on() and addListener() work in exactly the same manner: they create listeners for a specific type of event.




We prefer on() over addListener() as it requires less characters.




e.g.




var counter = new Counter();


counter.on('start', function() {


console.log('start event');


});

What does nodemon do and how should we install it?

nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application




Install globally via npm so all apps can use it