koa 筆記 運行錯誤

來源:互聯網
上載者:User

標籤:http   java   io   cti   代碼   javascript   

按照 示範的代碼 直接運行會出錯,大家需要調整方式。

http://koajs.cn/

要安裝以下

$ npm install -g n
$ n 0.11.12
$ node --harmony my-koa-app.js

注意:現在你直接在命令列中運行:node hello.js 是會報錯的。錯誤一般如下:
 
function
*respond(next){
        
^
SyntaxError: Unexpected token *
    at Module._compile
(module.js:439:25)
    at Object.Module._extensions..js
(module.js:474:10)
    at Module.load
(module.js:356:32)
    at Function.Module._load
(module.js:312:12)
    at Module.require
(module.js:364:17)
    at require
(module.js:380:17)
    at Object. (C:\Program
Files\nodejs\node_modules\koa\index.js:2:1
8)
    at
Module._compile (module.js:456:26)
    at
Object.Module._extensions..js (module.js:474:10)
    at
Module.load (module.js:356:32)

   這是因為  app.use(function
*(){ 語句中有一個 * ,這種方式被稱為generator functions ,一般寫作function *(){...} 的形式,在此類function
中可以支援ES6的一種yield概念(什嗎?你不知道什麼是yield...%$#@%....呵呵,好吧,其實我也不知道。不過我相信,我們後面會遇到它的,請耐心一點。)。於是我們需要讓這種新型的javascript方法可以編譯通過,就需要在運行node
命令的時候加上一個harmony參數(嗯,你知道什麼是河蟹吧。呵呵,對了。harmony的英文意思就是和諧,就是為了讓編譯器不僅可以接納舊的javascript文法也可以接納新時代的文法。)語句如下:
    
node --harmony hello.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.