Njs Flashcards
How do you start to create a program in nodeJs?
npm init
npm init -y
How do u install a package in nodeJS?
npm install ws
How do u run a nodeJS program?
node index.js
What is “g” in,
npm install -g
It installs a package in a globally available system directory. You can find that directory with following command,
npm root -g
It won’t install it in the current project directory. That has to be done again without “-g”.
After this you can install this package even if internet is not available. It will install the package from local system directory.
You may need root access.
Give an example of importing a package in nodeJS.
const WebSocket = require(‘ws’);
How do you printf from a NodeJs program?
console.log(“Hello World”);
How do you catch an event in Node.js
using “on” method of EventEmitter.
eventEmitter.on(“evnt_name”, handlerFunctionName);
How do u read a file in nodeJs?
var fs = require('fs'); fs.readFileSync("./filename.txt");
What is a good for NodeJs documentation.
www.nodejs.org/api
or
www.nodejs.org -> go to ‘Docs’ tab.
what are var, const and let variables in Node.js
var declares a variable.
const declares and defines a variable, that cannot be re-assigned.
let x = 5 defines a variable only at the block level, like a for loop counter etc.
Where to look for node.js packages
www.npmjs.com
How are constructor defined in nodejs class?
Inside class curly brackets, constructor(firstName, LastName) { ... }
How do you drive a child class from parent class in node.js?
class child_class extends parent_class { … }
How do u call constructor of parent class in child class in node.js?
super( parent class parameters);
How is event emitted in node.js?
eventEmitter.emit(‘event_name’); // No event parameters
Handle with the function like this,
function event_handler() { … }
Bind event_name and event_handler() as follows,
eventEmitter.on(‘event_name’, event_handler);
If event has parameters, event_handler function should also accept same parameters, see below,
eventEmitter.emit(‘event_name’, event_arg1, event_arg2);
event_handler(event_arg1, event_arg2) { … }
How is event emitter is created in node.js?
var events = require('events'); var eventEmitter = nre events.EventEmitter();
eventEmitter.on('event_name', function_name); eventEmitter.emit('event_name', arg1, arg2); function function_name(arg1, arg2) { ... }
How is util package used to make a child_class inherit a parent_class in node.js?
var util = require('util'); util.inherits(child_class, parent_class);
This can be used to make a class event emitter var events = require('events'); util.inherits(child_class, events.EventEmitter);
How to add time delay in a NodeJs program?
npm install sleep var sleep = require("sleep"); sleep.sleep(5); // 5 second delay
How do u access command line args in node.js
process.argv
This will return an array
process variable is available to all node.js programs.
How do you access process details in a node.js program
By using “process” variable available to all node.js programs
process.stdout.write(“Hello\n”);
is same as console.log(“hello”);
In node.js how to call a function 5 seconds from now?
function func1() {...} setTimeout(func1, 5000)
In node.js, How to call a function repeatedly every 3 seconds?
setInterval(func1, 3000);
How to exit a node.js program at any place in code?
process.exit();
In node.js, what package is needed to create a UDP socket?
const dgram = require('dgram'); const client_skt = dgram.createSocket('udp4'); const srvr_skt = dgram.createSocket('udp4');
How to create a buffer from a string in node.js?
var buf = new Buffer.from(“ABCD”);
How to create a buffer of a specified length in node.js?
var buf = Buffer.alloc(15);