Posts

What is the passport in Node.js?

  Passport.js is a simple, unobtrusive Node.js authentication middleware for Node.js. Passport.js can be dropped into any Express.js-based web application. Passport recognizes that each application has unique authentication requirements. Authentication mechanisms, known as strategies, are packaged as individual modules. Applications can choose which strategies to employ, without creating unnecessary dependencies. By default, if authentication fails, Passport will respond with a  401 Unauthorized  status, and any additional route handlers will not be invoked. If authentication succeeds, the  next  handler will be invoked and the req.user property will be set to the authenticated user.

What are the security mechanisms available in Node.js?

  We can secure our Node.js application in the following ways: Authentication  — Authentication is one of the primary security stages at which user is identified as permitted to access the application at all. Authentication verifies the user’s identity through one or several checks. In Node.js, authentication can be either session-based or token-based. In session-based authentication, the user’s credentials are compared to the user account stored on the server and, in the event of successful validation, a session is started for the user. Whenever the session expires, the user needs to log in again. In token-based authentication, the user’s credentials are applied to generate a string called a token which is then associated with the user’s requests to the server. Error Handling  — Usually, the error message contains the explanation of what’s actually gone wrong for the user to understand the reason. At the same time, when the error is related to the application code syntax...

What is a Callback function in Node.js?

Image
  Node.js, being an asynchronous platform, doesn’t wait around for things like file I/O to finish — Node.js uses callbacks. A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime. Callbacks are the foundation of Node.js. Callbacks give us an interface with which to say, “and when you’re done doing that, do all this.” This allows us to have as many IO operations as our OS can handle happening at the same time. For example, in a web server with hundreds or thousands of pending requests with multiple blocking queries, performing the blocking queries asynchronously gives you the ability to be able to continue working and not just sit still and wait until the blocking operations come back.

What is the use of DNS module in Node.js?

  dns   module which provide underlying system's name resolution and DNS look up facilities. DNS module consists of an asynchronous network wrapper. The most commonly used functions in DNS module are: dns.lookup(adress, options, callback)  - The dns lookup method takes any website address as its first parameter and returns the corresponding first IPV4 or IPV6 record. The options parameter can be an integer or object. If no options are provided both IPV4 and IPV6 are valid inputs. The third parameter is the callback functions. dns.lookupservice(address, port, callback)  - This function converts any physical address such as “www.knowledgehills.com” to array of record types. The record types are specified by the second parameter “rrbyte”. Finally the third method is the callback function. dns.getServers()  - This function returns an array of IP address strings, formatted according to rfc5952, that are currently configured for DNS resolution. A string will include a...

What is the use of Timers is Node.js?

The Timers module in Node.js contains functions that execute code after a set period of time. Timers do not need to be imported via  require() , since all the methods are available globally to emulate the browser JavaScript API. The Node.js API provides several ways of scheduling code to execute at some point after the present moment. The functions below may seem familiar, since they are available in most browsers, but Node.js actually provides its own implementation of these methods. Node.js Timer provides   setTimeout() ,   setImmediate()   and   setInterval .  

Is cryptography supported in Node.js? What is crypto in Node.js? How do you cipher the secured information in Node.js?

Image
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 2 3 4 const crypto = require 'crypto' ); const secret = 'akerude' ; const hash = crypto.createHmac( 'swaEdu' , secret).update( 'Welcome to Edureka' ).digest( 'hex' ); console.log(hash); The  crypto  module in Node.js provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign and verify functions.  

What is the difference between readFile vs createReadStream in Node.js?

  readFile   — is for asynchronously reads the entire contents of a file.  It will read the file completely into memory before making it available to the User.   readFileSync   is synchronous version of   readFile . It is a memory-intensive process and in the case of large files, the processing can be very slow.  createReadStream  — It will read the file in chunks of the default size 64 kb which is specified before hand. 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.