课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

基本的 HTTP 服务器

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

问题

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

解决方案

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

开始 'hi\n'

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

  1. http = require 'http'
  2. server = http.createServer (req, res) -> res.end 'hi\n'
  3. server.listen 8000

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

  1. $ curl -D - http://localhost:8000/
  2. HTTP/1.1 200 OK
  3. Connection: keep-alive
  4. Transfer-Encoding: chunked
  5. hi

发生什么了?

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

  1. http = require 'http'
  2. server = http.createServer (req, res) ->
  3. console.log req.method, req.url
  4. data = 'hi\n'
  5. res.writeHead 200,
  6. 'Content-Type': 'text/plain'
  7. 'Content-Length': data.length
  8. res.end data
  9. server.listen 8000

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

  1. $ coffee http-server.coffee
  2. GET /
  3. GET /coffee
  4. GET /user/1337

得到的东西

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

  1. http = require 'http'
  2. store = # we'll use a simple object as our store
  3. foo: 'bar'
  4. coffee: 'script'
  5. server = http.createServer (req, res) ->
  6. console.log req.method, req.url
  7. value = store[req.url[1..]]
  8. if not value
  9. res.writeHead 404
  10. else
  11. res.writeHead 200,
  12. 'Content-Type': 'text/plain'
  13. 'Content-Length': value.length + 1
  14. res.write value + '\n'
  15. res.end()
  16. server.listen 8000

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

  1. $ curl -D - http://localhost:8000/coffee
  2. HTTP/1.1 200 OK
  3. Content-Type: text/plain
  4. Content-Length: 7
  5. Connection: keep-alive
  6. script
  7. $ curl -D - http://localhost:8000/oops
  8. HTTP/1.1 404 Not Found
  9. Connection: keep-alive
  10. Transfer-Encoding: chunked

使用你的头文件

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

  1. http = require 'http'
  2. # known mime types
  3. [any, json, xml] = ['*/*', 'application/json', 'text/xml']
  4. # gets a value from the db in format [value, contentType]
  5. get = (store, key, format) ->
  6. value = store[key]
  7. throw 'Unknown key' if not value
  8. switch format
  9. when any, json then [JSON.stringify({ key: key, value: value }), json]
  10. when xml then ["<key>#{ key }</key>\n<value>#{ value }</value>", xml]
  11. else throw 'Unknown format'
  12. store =
  13. foo: 'bar'
  14. coffee: 'script'
  15. server = http.createServer (req, res) ->
  16. console.log req.method, req.url
  17. try
  18. key = req.url[1..]
  19. [value, contentType] = get store, key, req.headers.accept
  20. code = 200
  21. catch error
  22. contentType = 'text/plain'
  23. value = error
  24. code = 404
  25. res.writeHead code,
  26. 'Content-Type': contentType
  27. 'Content-Length': value.length + 1
  28. res.write value + '\n'
  29. res.end()
  30. server.listen 8000

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

  1. $ curl http://localhost:8000/
  2. Unknown key
  3. $ curl http://localhost:8000/coffee
  4. {"key":"coffee","value":"script"}
  5. $ curl -H "Accept: text/xml" http://localhost:8000/coffee
  6. <key>coffee</key>
  7. <value>script</value>
  8. $ curl -H "Accept: image/png" http://localhost:8000/coffee
  9. Unknown format

你需要有所返回

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

  1. http = require 'http'
  2. # known mime types
  3. [any, json, xml] = ['*/*', 'application/json', 'text/xml']
  4. # gets a value from the db in format [value, contentType]
  5. get = (store, key, format) ->
  6. value = store[key]
  7. throw 'Unknown key' if not value
  8. switch format
  9. when any, json then [JSON.stringify({ key: key, value: value }), json]
  10. when xml then ["<key>#{ key }</key>\n<value>#{ value }</value>", xml]
  11. else throw 'Unknown format'
  12. # puts a value in the db
  13. put = (store, key, value) ->
  14. throw 'Invalid key' if not key or key is ''
  15. store[key] = value
  16. store =
  17. foo: 'bar'
  18. coffee: 'script'
  19. # helper function that responds to the client
  20. respond = (res, code, contentType, data) ->
  21. res.writeHead code,
  22. 'Content-Type': contentType
  23. 'Content-Length': data.length
  24. res.write data
  25. res.end()
  26. server = http.createServer (req, res) ->
  27. console.log req.method, req.url
  28. key = req.url[1..]
  29. contentType = 'text/plain'
  30. code = 404
  31. switch req.method
  32. when 'GET'
  33. try
  34. [value, contentType] = get store, key, req.headers.accept
  35. code = 200
  36. catch error
  37. value = error
  38. respond res, code, contentType, value + '\n'
  39. when 'POST'
  40. value = ''
  41. req.on 'data', (chunk) -> value += chunk
  42. req.on 'end', () ->
  43. try
  44. put store, key, value
  45. value = ''
  46. code = 200
  47. catch error
  48. value = error + '\n'
  49. respond res, code, contentType, value
  50. server.listen 8000

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

  1. $ curl -D - http://localhost:8000/cookie
  2. HTTP/1.1 404 Not Found # ...
  3. Unknown key
  4. $ curl -D - -d "monster" http://localhost:8000/cookie
  5. HTTP/1.1 200 OK # ...
  6. $ curl -D - http://localhost:8000/cookie
  7. HTTP/1.1 200 OK # ...
  8. {"key":"cookie","value":"monster"}

讨论

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

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

练习

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

  1. server = layer.createServer
  2. 'GET /': (req, res) ->
  3. ...
  4. 'GET /page': (req, res) ->
  5. ...
  6. 'PUT /image': (req, res) ->
  7. ...
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号