×
CoffeeScript 关于

CoffeeScript 语法

服务端和客户端的代码重用CoffeeScript 比较范围CoffeeScript 嵌入 JavaScriptCoffeeScript For 循环

CoffeeScript 类和对象

CoffeeScript 对象的链式调用CoffeeScript 类方法和实例方法CoffeeScript 类变量和实例变量CoffeeScript 克隆对象CoffeeScript 类的混合CoffeeScript 创建对象字面值CoffeeScript type 函数

CoffeeScript 字符串

CoffeeScript 大写单词首字母CoffeeScript 查找子字符串CoffeeScript 生成唯一 IDCoffeeScript 字符串插值CoffeeScript 字符串小写转换CoffeeScript 匹配字符串CoffeeScript 重复字符串CoffeeScript 拆分字符串CoffeeScript 清理字符串前后CoffeeScript 字符串大写转换

CoffeeScript 数组

CoffeeScript 检查变量是否数组CoffeeScript 将数组连接CoffeeScript 数组创建对象词典CoffeeScript 数组创建字符串CoffeeScript 定义数组范围CoffeeScript 筛选数组CoffeeScript 列表推导CoffeeScript 映射数组CoffeeScript 数组最大值CoffeeScript 归纳数组CoffeeScript 删除数组相同元素CoffeeScript 反转数组CoffeeScript 打乱数组中的元素CoffeeScript 检测每个元素CoffeeScript 用数组来交换变量CoffeeScript 对象数组类似 Python 的 zip 函数

CoffeeScript 日期和时间

CoffeeScript 计算复活节的日期CoffeeScript 计算感恩节日期计算两个日期中间的天数CoffeeScript 算1个月中最后1天CoffeeScript 找到上/下一个月CoffeeScript 计算月球的相位

CoffeeScript 数学

CoffeeScript 数学常数更快的 Fibonacci 算法平方根倒数快速算法生成可预测的随机数CoffeeScript 生成随机数CoffeeScript 转换弧度和度CoffeeScript 一个随机整数函数CoffeeScript 指数对数运算CoffeeScript 方法CoffeeScript 去抖动函数CoffeeScript 当函数括号不可选CoffeeScript 递归函数CoffeeScript 提示参数CoffeeScript 元编程检测与构建丢失函数CoffeeScript 扩展内置对象

CoffeeScript jQuery

CoffeeScript AJAXCoffeeScript 回调绑定CoffeeScript 创建 jQuery 插件CoffeeScript AJAXCoffeeScript 无jQuery的Ajax

CoffeeScript 正则表达式

CoffeeScript 使用 HeregexesHTML实体替换 HTML 标签CoffeeScript 替换子字符串CoffeeScript 查找子字符串CoffeeScript 网络CoffeeScript 客户端CoffeeScript HTTP 客户端CoffeeScript 基本HTTP服务器CoffeeScript 服务器CoffeeScript 双向客户端CoffeeScript 双向服务器

CoffeeScript 设计模式

CoffeeScript 适配器模式CoffeeScript 桥接模式CoffeeScript 生成器模式CoffeeScript 命令模式CoffeeScript 修饰模式CoffeeScript 工厂方法模式CoffeeScript 解释器模式CoffeeScript 备忘录模式CoffeeScript 观察者模式CoffeeScript 单件模式CoffeeScript 策略模式CoffeeScript 模板方法模式

CoffeeScript 数据库

CoffeeScript MongoDBCoffeeScript SQLite

CoffeeScript 测试

使用 Jasmine 测试使用 Nodeunit 测试

CoffeeScript 基本的 HTTP 服务器


问题

你想在网络上创建一个HTTP服务器。在这个方法中,我们将逐步从最小的服务器成为一个功能键值存储。

解决方案

我们将使用node.js HTTP库并在Coffeescript中创建最简单的web服务器。

开始 'hin'

我们可以通过导入node.js HTTP模块开始。这会包含createServer,一个简单的请求处理程序返回HTTP服务器。我们可以使用该服务器监听TCP端口。

http = require 'http'
server = http.createServer (req, res) -> res.end 'hin'
server.listen 8000

要运行这个例子,只需放在一个文件中并运行它。你可以用ctrl-c终止它。我们可以使用curl命令测试它,可用在大多数*nix平台:

$ curl -D - http://localhost:8000/
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked

hi

发生什么了?

让我们一点点来反馈服务器上发生的事情。这时,我们可以友好的对待用户并提供他们一些HTTP头文件。

http = require 'http'

server = http.createServer (req, res) ->
    console.log req.method, req.url
    data = 'hin'
    res.writeHead 200,
        'Content-Type':     'text/plain'
        'Content-Length':   data.length
    res.end data

server.listen 8000

再次尝试访问它,但是这一次使用不同的URL路径,比如http://localhost:8000/coffee。你会看到这样的服务器控制台:

$ coffee http-server.coffee 
GET /
GET /coffee
GET /user/1337

得到的东西

假如我们的网络服务器能够保存一些数据会怎么样?我们将在通过GET方法请求检索的元素中设法想出一个简单的键值存储。并提供一个关键路径,服务器将请求返回相应的值,如果不存在则返回404错误。

http = require 'http'

store = # we'll use a simple object as our store
    foo:    'bar'
    coffee: 'script'

server = http.createServer (req, res) ->
    console.log req.method, req.url

    value = store[req.url[1..]]

    if not value
        res.writeHead 404
    else
        res.writeHead 200,
            'Content-Type': 'text/plain'
            'Content-Length': value.length + 1
        res.write value + 'n'

    res.end()

server.listen 8000

我们可以试试几种url,看看它们如何回应:

$ curl -D - http://localhost:8000/coffee
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 7
Connection: keep-alive

script

$ curl -D - http://localhost:8000/oops
HTTP/1.1 404 Not Found
Connection: keep-alive
Transfer-Encoding: chunked

使用你的头文件

text/plain是站不住脚的。如果我们使用application/json或text/xml会怎么样?同时,我们的存储检索过程也可以用一点重构——一些异常的抛出&处理怎么样? 来看看我们能想出什么:

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 }n#{ value }", xml]
        else throw 'Unknown format'

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

server = http.createServer (req, res) ->
    console.log req.method, req.url

    try
        key = req.url[1..]
        [value, contentType] = get store, key, req.headers.accept
        code = 200
    catch error
        contentType = 'text/plain'
        value = error
        code = 404

    res.writeHead code,
        'Content-Type': contentType
        'Content-Length': value.length + 1
    res.write value + 'n'
    res.end()

server.listen 8000

这个服务器仍然会返回一个匹配给定键的值,如果不存在则返回404错误。但它根据标头Accept将响应在JSON或XML结构中。可亲眼看一下:

$ curl http://localhost:8000/
Unknown key

$ curl http://localhost:8000/coffee
{"key":"coffee","value":"script"}

$ curl -H "Accept: text/xml" http://localhost:8000/coffee
coffee
script

$ curl -H "Accept: image/png" http://localhost:8000/coffee
Unknown format

你需要有所返回

我们的最后一步是提供客户端存储数据的能力。我们将通过监听POST请求来保持RESTiness。

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 }n#{ 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': data.length
    res.write data
    res.end()

server = http.createServer (req, res) ->
    console.log req.method, req.url
    key = req.url[1..]
    contentType = 'text/plain'
    code = 404

    switch req.method
        when 'GET'
            try
                [value, contentType] = get store, key, req.headers.accept
                code = 200
            catch error
                value = error
            respond res, code, contentType, value + 'n'

        when 'POST'
            value = ''
            req.on 'data', (chunk) -> value += chunk
            req.on 'end', () ->
                try
                    put store, key, value
                    value = ''
                    code = 200
                catch error
                    value = error + 'n'
                respond res, code, contentType, value

server.listen 8000

在一个POST请求中注意数据是如何接收的。通过在“数据”和“结束”请求对象的事件中附上一些处理程序,我们最终能够从客户端缓冲和保存数据。

$ curl -D - http://localhost:8000/cookie
HTTP/1.1 404 Not Found # ...
Unknown key

$ curl -D - -d "monster" http://localhost:8000/cookie
HTTP/1.1 200 OK # ...

$ curl -D - http://localhost:8000/cookie
HTTP/1.1 200 OK # ...
{"key":"cookie","value":"monster"}

讨论

给http.createServer一个函数 (request,response) - >…… 它将返回一个服务器对象,我们可以用它来监听一个端口。让服务器与request和response对象交互。使用server.listen 8000监听端口8000。

在这个问题上的API和整体信息,参考node.js httphttps文档页面。此外,HTTP spec可能派上用场。

练习

在服务器和开发人员之间创建一个层,允许开发人员做类似的事情:

server = layer.createServer
    'GET /': (req, res) ->
        ...
    'GET /page': (req, res) ->
        ...
    'PUT /image': (req, res) ->
        ...

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)