Node.js Interview Questions - Advanced Level

Ace your homework & exams now with Quizwiz!

What do you understand by an Event Emitter in Node.js?

EventEmitter is a Node.js class that includes all the objects that are capable of emitting events. These objects contain an eventEmitter.on() function through which more than one function can be attached to the named events that are emitted by the object. Whenever an EventEmitter object throws an event, all the attached functions to that specific event are invoked synchronously. Below code shows how to us the EventEmitter in your application: 1 const EventEmitter = require('events'); 2 class MyEmitter extends EventEmitter { } 3 const myEmitter = new MyEmitter(); 4 myEmitter.on('event', () => { 5 console.log('an event occurred!'); 6 }); 7 myEmitter.emit('event');

Explain the reason as to why Express 'app' and 'server' must be kept separate.

Explain the reason as to why Express 'app' and 'server' must be kept separate. Express 'app' and 'server' must be kept separate as by doing this, you will be separating the API declaration from the network related configuration which benefits in the below listed ways: - It allows testing the API in-process without having to perform the network calls - Faster testing execution - Getting wider coverage metrics of the code - Allows deploying the same API under flexible and different network conditions - Better separation of concerns and cleaner code API declaration should reside in app.js: 1 var app = express(); 2 app.use(bodyParser.json()); 3 app.use("/api/events", events.API); 4 app.use("/api/forms", forms); Server network declaration should reside in /bin/www: 1 var app = require('../app'); 2 var http = require('http'); 3 //Get port from environment and store in Express 4 var port = normalizePort(process.env.PORT || '8000'); 5 app.set('port', port); 6 //Create HTTP server. 7 var server = http.createServer(app);

What is the use of NODE_ENV?

If the project is in the production stage, Node.js promotes the convention of making use of NODE_ENV variable to flag it. This helps in taking better judgment during the development of the projects. Also, when you set your NODE_ENV to production, your application tends to perform 3 times faster.

Explain the concept of Punycode in Node.js?

In Node.js, Punycode is an encoding syntax that is used for converting Unicode (UTF-8) string of characters into a basic ASCII string of characters. It is important as the hostnames can only understand the ASCII characters. Thus, Node.js version 0.6.2 onwards, it was bundled up with the default Node package. If you want to use it with any previous versions, you can easily do that by using the following code: Syntax: 1 punycode = require('punycode');

Describe the exit codes of Node.js.

In Node.js, exit codes are a set of specific codes which are used for finishing a specific process. These processes can include the global object as well. Below are some of the exit codes used in Node.js: - Uncaught fatal exception - Unused - Fatal Error - Internal Exception handler Run-time failure - Internal JavaScript Evaluation Failure

Does Node.js provide any Debugger?

Node.js do provide a simple TCP based protocol and debugging client that comes built-in. In order to debug your JavaScript file, you can use the below debug argument followed by js file name that you want to debug. Syntax: 1 node debug [script.js | -e "script" | <host> : <port> ]

List down the various timing features of Node.js.

Node.js provides a Timers module which contains various functions for executing the code after a specified period of time. Below I have listed down the various functions provided by this module: - setTimeout/clearTimeout - Used to schedule code execution after a designated amount of milliseconds - setInterval/clearInterval - Used to execute a block of code multiple times - setImmediate/clearImmediate - Used to execute code at the end of the current event loop cycle - process.nextTick - Used to schedule a callback function that needs to be invoked in the next iteration of the Event Loop

Differentiate between readFile vs createReadStream in Node.js?

Node.js provides two ways to read and execute files which are using readFile and CreateStream. readFile() is a fully buffered process which returns the response only when the complete file is pushed into the buffer and is read. It is a memory intensive process and in case of large files, the processing can be very slow. Whereas createReadStream is a partially buffered which treats the entire process as an event series. The entire file is split into chunks which are then processed and sent back as a response one by one. Once done, they are finally removed from the buffer. Unlike readFile, createReadStream is really effective for the processing of the large files.

Differentiate between Node.js vs Ajax?

The most basic difference between Node.js and Ajax that, Node.js is a server-side JavaScript whereas Ajax is a client-side technology. In simpler terms, Ajax is mostly used for updating or modifying the webpage contents without having to refresh it. On the other hand, Node.js is required to develop the server software that are typically executed by the servers instead of the web browsers.

Is cryptography supported in Node.js?

Yes, Node.js does support cryptography through a module called Crypto. This module provides various cryptographic functionalities like cipher, decipher, sign and verify functions, a set of wrappers for open SSL's hash HMAC etc. For example: Syntax: 1 const crypto = require'crypto'); 2 const secret = 'akerude'; 3 const hash = crypto.createHmac('swaEdu', secret).update('Welcome to Edureka').digest('hex'); 4 console.log(hash);


Related study sets

Nutrition chapter 1 reading assignment

View Set

Lezione 10 Vocab (p. 238) + La Puglia

View Set

Life and Health Practice Exam #1 XCEL

View Set

Advantages/Challenges of Multiple Domestic and International Distribution Centers

View Set