LoginSignup
18
20

More than 5 years have passed since last update.

CoffeeScriptでkoaを使う

Posted at

話題のkoaをCoffeeScriptで試してみようと思ったところ

function *() { } をどのように書いたらいいのかわからず
https://github.com/jashkenas/coffee-script/pull/3240
にpull requestがあったので早速試してみた

git clone https://github.com/alubbe/coffee-script
cd coffee-script 
npm install 

node v0.11では yieldを有効にするために bin/coffee に--harmonyを付ける

#!/usr/bin/env node --harmony

公式のサンプルをCofeeScriptで書いてみた

koa = require('koa')
app = koa()

app.use ->*
  @body = "Hello, World"

app.listen 3000
koa = require('koa')
app = koa()

# x-response-time
app.use (next) ->*
  start = new Date
  yield next
  ms = new Date - start
  @set 'X-Response-Time', "#{ms}ms"

# logger
app.use (next) ->*
  start = new Date
  yield next
  ms = new Date - start
  console.log "#{@method} #{@url} - #{ms}"

# response
app.use (next) ->*
  @body = 'Hello World'

app.listen 3000
18
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
20