coffeescript 搭建http服务示例代码

代码语言:coffeescript

所属分类:文件

代码描述:coffeescript 搭建http服务示例代码

代码标签: 服务 示例

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

http = require 'http'

# known mime types
[any, json, xml] = ['*/*', 'application/json', 'text/xml']

# gets a value from the db in format [value, contentType]
get = (store, key, format) ->
    value = store[key]
    throw 'Unknown key' if not value
    switch format
        when any, json then [JSON.stringify({ key: key, value: value }), json]
        when xml then ["<key>#{ key }</key>\n<value>#{ value }</value>", xml]
        else throw 'Unknown format'

# puts a value in the db
put = (store, key, value) ->
    throw 'Invalid key' if not key or key is ''
    store[key] = value

store =
    foo:    'bar'
    coffee: 'script'

# helper function that responds to the client
respond = (res, code, contentType, data) ->
    res.writeHead code,
        'Content-Type': contentType
        'Content-Length.........完整代码请登录后点击上方下载按钮下载查看

网友评论0