当前位置:K88软件开发文章中心编程语言JavaScriptCoffeeScript → 文章内容

CoffeeScript 基本的 HTTP 服务器

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:34:04

由 珍珍阿姨 创建,Carrie 最后一次修改 2016-08-12 基本的 HTTP 服务器问题你想在网络上创建一个HTTP服务器。在这个方法中,我们将逐步从最小的服务器成为一个功能键值存储。解决方案我们将使用node.js HTTP库并在Coffeescript中创建最简单的web服务器。开始 'hi\n'我们可以通过导入node.js HTTP模块开始。这会包含createServer,一个简单的请求处理程序返回HTTP服务器。我们可以使用该服务器监听TCP端口。http = require 'http'server = http.createServer (req, res) -> res.end 'hi\n'server.listen 8000要运行这个例子,只需放在一个文件中并运行它。你可以用ctrl-c终止它。我们可以使用curl命令测试它,可用在大多数*nix平台:$ curl -D - http://localhost:8000/HTTP/1.1 200 OKConnection: keep-aliveTransfer-Encoding: chunkedhi发生什么了?让我们一点点来反馈服务器上发生的事情。这时,我们可以友好的对待用户并提供他们一些HTTP头文件。http = require 'http'server = http.createServer (req, res) -> console.log req.method, req.url data = 'hi\n' res.writeHead 200, 'Content-Type': 'text/plain' 'Content-Length': data.length res.end dataserver.listen 8000再次尝试访问它,但是这一次使用不同的URL路径,比如http://localhost:8000/coffee。你会看到这样的服务器控制台:$ coffee http-server.coffee GET /GET /coffeeGET /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/coffeeHTTP/1.1 200 OKContent-Type: text/plainContent-Length: 7Connection: keep-alivescript$ curl -D - http://localhost:8000/oopsHTTP/1.1 404 Not FoundConnection: keep-aliveTransfer-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>#{ key }</key>\n<value>#{ value }</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<key>coffee</key><value>script</value>$ curl -H "Accept: image/png" http://localhost:8000/coffeeUnknown 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>#{ key }</key>\n<value>#{ value }</value>", xml] else throw 'Unknown format'# puts a value in the dbput = (store, key, value) -> throw 'Invalid key' if not key or key is '' store[key] = valuestore = foo: 'bar' coffee: 'script'# helper function that responds to the clientrespond = (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

[1] [2]  下一页


CoffeeScript 基本的 HTTP 服务器