Skip to main content

NodeJS

Question : Why You should use Node JS?
Answer : Following are the major factor influencing the use of the NodeJS
Popularity : The popularity can be important factor, as it has more user base and hence solution 
of any common problem faced by developer can found easily online, without any professional help.

JavaScript at all levels of the stack : A common language for frontend and backend offers several potential benefits:

  • The same programming staff can work on both ends of the wire
  • Code can be migrated between server and client more easily
  • Common data formats (JSON) exist between server and client
  • Common software tools exist for server and client
  • Common testing or quality reporting tools for server and client
  • When writing web applications, view templates can be used on both sides


Leveraging Google's investment in V8 Engine.

Leaner, asynchronous, event-driven model


Microservice architecture


Question : example of node JS code?
Answer : 
const fs = require('fs');
const util = require('util');
const fs_readdir = util.promisify(fs.readdir);

( async () => {
    const files = await fs_readdir('.');
    for(let file of files ){
        console.log(file);
    }

})();

Question :  Example to read the content from the command line
Answer  : 
code
console.log(process.argv[2] ? process.argv[2] : 'No Arguments passed');

command line
/usr/local/bin/node /Users/vcmishra/central-api/db> test.js hello

Question : create a basic server?
Answer

const http = require('http');
http.createServer((req,res)=>{
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, World!\n');

}).listen(1234,'127.0.0.1');

Question : Is require a async or sync?
Answer : sync.

Question : Give example of module.exports
Answer :
File 1 : the methods are created
module.exports.hello = ()=>{
console.log('hello');
};

module.exports.hi = ()=>{
console.log('hi');
};

File 2 : it is imported and used
const greetUtil = require('./test');
greetUtil.hello();
greetUtil.hi();

The function can directly called if imported this way

const hello = require('./test').hello;
const hi = require('./test').hi;
hello();
hi();


or alternatively can be called like as follow
require('./test').hello()
require('./test').hi()

Another way is

File 1 :
module.exports = {
hello: () => {
console.log('hello');
},
hi: () => {
console.log('hi');
}
};

File 2 :
There may not be needing any change, but alternativly using the es6 destrcutring you can also use as follows :

const { hello, hi } = require('./test');
hi();
hello();


Another example using destructuring
File 1
exports.util = {
hello: () => {
console.log('hello');
},

hi: () => {
console.log('hi');
}
};


exports.autil = {
hello: () => {
console.log('a hello');
},

hi: () => {
console.log('a hi');
}
};


File 2 :
const { hello, hi } = require('./test').util;
const { hello : aHello , hi : aHi } = require('./test').autil;

hi();
hello();

aHi();
aHello();

Pay attention to the second line where, we are renaming the hello and hi from the autil to avoid the conflict between previously imported hi and hello from util.








Comments

Popular posts from this blog

12 - HTML 5 and CSS

HTML 5 Question : If I do not put <! DOCTYPE html> will HTML 5 work? Answer : No, browser will not be able to identify that it’s a HTML document and HTML 5 tags will not function properly. Diff between HTML 5 Layout and HTML 4 or previous HTML? Answer : A typical web page has headers, footers, navigation, central area and side bars. Now if we want to represent the same in HTML 4 with proper names to the HTML section we would probably use a DIV tag. But in HTML 5 they have made it more clear by creating element names for those sections which makes your HTML more readable. Below are more details of the HTML 5 elements which form the page structure. <header> : Represents header data of HTML. <footer> : Footer section of the page. <nav> : Navigation elements in the page. <article> : Self-contained content. <section> : Used inside article to define sections or group content in to sections. <aside> : Represent side bar contents of a...

Collections JAVA

Collection Question:Comparable and Comparator? Comparable Comparator Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc. Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc. Comparable affects the original class i.e. actual class is modified. Comparator doesn't affect the original class i.e. actual class is not modified. Comparable provides compareTo() method to sort elements. Comparator provides compare() method to sort elements. Comparable is found in java.lang package. Comparator is found in java.util package. We can sort the list elements of Comparable type byCollections.sort(List) method. We can sort the list elements of Comparator type   byCollections.sort(List,...

OOAP -Javascript

JAVASCRIPT Question : How to find the index of an element from the JavaScript Array? Answer : Arrayname.indexOf(value); Question : How to remove an element form the JavaScript Array? Answer : Arrayname.splice(indexOfElement,1); Question : How can you convert a String to an Object? Answer : provided the String is valid JSON String we can convert it using JSON.parse(VALID JSON TEXT) e.g. var validText = '{"name" : "vikash" , "success":true, "Address" : {"Building" : "Building_X", "Flat" : "12"}}'; var obj = JSON.parse(validText); Now we can access the data in this way obj . name or obj. success or obj. Address . Building or obj. Address . Flat What is Event Bubbling and Capturing? Answer :  On the Introduction to events page I asked a question that at first sight seems incomprehensible: “If an element and one of its ancestors have an event handler for the same event, which one sho...