標籤: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