koa2 entry --01.ES6 simple review, koa2 installation and examples

1.ES6 simple review

/ * The let and const: let for defining a variable block scope, const define a constant * / 
    the let A = 'Test' ; 
    const B = 2 ; 
   
    / * shorthand object properties and methods:   * / 
    / * the ES5 written: * / 
    var name = 'Test' ;
     var es5Ojb = { 
        name: name, 
        FUNC: function () { 

        } 
    }; 
     / * for ES6 object properties and methods written: * / 
    var obj = { 
        name, 
        FUNC () { 

        } 
    }; 

    / * arrows function * / 
    / *   the ES5 anonymous function written:* / 
    Function es5Test () {
         return  function () { 
            the console.log ( 'es5Test' ); 
        }; 
    }; 
    / * for ES6 wording: * / 
    function es6Test () {
         return () => { 
            the console.log ( 'es6Test' ); 
        }; 
    ;} 

    / * with Promise callback processing * / 
    // for ES6: a function parameter as a function of the data type, the body of the function call parameter, and parameter passing, a function to execute the function in vitro, and write into the function, such as the following 
    var P = new new Promise ((reslove, Reject) => {
         // mimic asynchronous function 
        setTimeout (() =>{
             Var name = 'John Doe' ;
             IF (Math.random () <0.7 ) 
            reslove (name); 
            the else 
            reject ( 'fail' ); 
            
        }, 1000 ); 
    }); 
    / * Data to pass into the reject or reslove parameters * / 
    p.then ((Data) => { 
        the console.log (Data); 
    });

2, koa2 installation: Open cmd within the folder, enter: npm install --save koa, Note: Only node.js version can only be used in 7.2 above, using the node -v command to view node version

const koa = require('koa');
const app = new koa();

app.use( async(ctx)=>{
    
    ctx.body = 'hello koa2';
});


app.listen(80);

effect:

 

 

          

 

   

Guess you like

Origin www.cnblogs.com/Liqian-Front-End-Engineer/p/11760500.html