Posts

ES5 to ES6

function to => Let's start with the basic ES5 function. function sayHello (name){ console . log ( "Hello" + name) ; } Let us assign the above function to a variable sayHello , var sayHello = function (name){ console . log ( "Hello " +name) ; } Let's transform es5 function to es6 function, Step 1 : Get rid of function var sayHello = (name){ Step 2 : introduce a new symbol => which is going to replace the word  function var sayHello = =>(name) Step 3 : Move the => after the () var sayHello = (name) => That's it, now look at the transformation of the es5 to es6 var sayHello = (name) => { console . log ( "Hello " +name) ; } That was beginner level stuff, let's make it shorter like a pro, by getting rid of parentheses around " name ", so to add your knowledge level, you don't need enclose your argument with () , if there is only one argument, you need () , only

Enable CORS or Pre flight requests at NodeJS

Cros Origin Resource Sharing This is how we can Enable CORS const app = express (); /**   * Changes made to enable CORS   */ const allowCORS = ( req , res , next ) => {     res . header ( 'Access-Control-Allow-Origin' , '*' );     //Method Supported     res . header ( 'Access-Control-Allow-Methods' , 'GET,PUT,POST,DELETE' );     //If you have additional Headers which needed to be allowed     res . header ( 'Access-Control-Allow-Headers' , 'Content-Type, Authorization, token' );     // intercept OPTIONS method, as all the preflight request first send the options method, and based     // on the success, it will further allows the actual call.     if ( 'OPTIONS' == req . method ) {         res .sendStatus( 200 );     } else {         next ();} }; //Allow cross domain. app . use ( allowCORS );