链式方法在coffeescript上失败了

时间:2014-09-03 16:29:09

标签: coffeescript

server1.coffee

http = require 'http'
http.createServer (req, resp) ->
  resp.writeHead 200
  resp.end 'Hi !'
.listen 1337, '127.0.0.1'

server2.coffee

require 'http'
.createServer (req, resp) ->
  resp.writeHead 200
  resp.end 'Hi !'
.listen 1337, '127.0.0.1'

server1.coffee工作但是为什么server2.coffee得到以下错误,

TypeError: Object http has no method 'createServer'

1 个答案:

答案 0 :(得分:2)

它链接在'http',即string

如果您希望在链接语法中正确评估它,则需要围绕整个require 'http'表达式使用括​​号。