Christian-health / StudyNote2017

2017年学习笔记
0 stars 0 forks source link

node-restify 后端桩的实现原理 #3

Open Christian-health opened 7 years ago

Christian-health commented 7 years ago

原文地址:https://github.com/restify/node-restify

概述

restify is a framework, utilizing connect style middleware for building REST APIs. For full details, see http://restify.com

restify是一个框架,利用连接风格的中间件来构建REST API。 有关详细信息,请参阅http://restify.com

Christian-health commented 7 years ago

使用

Quick Start

Setting up a server is quick and easy. Here is a barebones echo server: 设置服务器是快速和容易的。 这是一个精简(barebones 皮包骨的人,精简 )回声服务器:

var restify = require('restify');//导入

function respond(req, res, next) {
  res.send('hello ' + req.params.name);
  next();
}

var server = restify.createServer();//createServer
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);

server.listen(8080, function() {  //listen
  console.log('%s listening at %s', server.name, server.url);
});

Try hitting that with the following curl commands to get a feel for what restify is going to turn that into: 试着使用下面的curl命令,来感受一下restify将会对发送来的请求做出什么样的转换。

-i/--include     输出时包括protocol头信息
-s/--slient      减少输出的信息,比如进度
-H/--header  <header> 指定请求头参数

$ curl -is http://localhost:8080/hello/mark -H 'accept: text/plain'
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 10
Date: Mon, 31 Dec 2012 01:32:44 GMT
Connection: keep-alive

hello mark

$ curl -is http://localhost:8080/hello/mark
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 12                                    
 //长度为12,因为没有指定-H所以接受到的是带有双引号的字符串,所以长多多了2
Date: Mon, 31 Dec 2012 01:33:33 GMT
Connection: keep-alive

"hello mark"

//这里使用-H指定了http请求头,可以直接去查这个请求头的信息
$ curl -is http://localhost:8080/hello/mark -X HEAD -H 'connection: close'
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 12
Date: Mon, 31 Dec 2012 01:42:07 GMT
Connection: close

HTTP头信息:http://blog.chinaunix.net/uid-10540984-id-3130355.html Connection表示连接状态 请求:close(告诉WEB服务器或者代理服务器,在完成本次请求的响应后,断开连接,不要等待本次连接的后续请求了)。

Note that by default, curl uses Connection: keep-alive. In order to make the HEAD method return right away, you’ll need to pass Connection: close. 请注意,默认情况下,curl使用Connection:keep-alive。 为了使HEAD方法立即返回,您需要通过Connection:close。

Since curl is often used with REST APIs, restify’s plugins include a plugin to work around this idiosyncrasy in curl. The plugin checks whether the user agent is curl. If it is, it sets the Connection header to “close” and removes the “Content-Length” header.

由于curl经常与REST API一起使用,所以resify的插件包括一个插件来解决这个特殊curl问题。 插件检查用户代理是否是curl。 如果是,它将Connection头设置为“close”,并删除“Content-Length”头。

server.pre(restify.plugins.pre.userAgentConnection());

Sinatra style handler chains Sinatra风格的处理链

Sinatra 框架:https://github.com/sinatra/sinatra Like many other Node.js based REST frameworks, restify leverages a Sinatra style syntax for defining routes and the function handlers that service those routes: 像许多其他基于Node.js的REST框架一样,重新定义利用Sinatra样式语法来定义路由和为这些路由服务的函数处理程序:

server.get('/', function(req, res, next) {
  res.send('home')
  return next();
});

server.post('/foo',
  function(req, res, next) {
    req.someData = 'foo';
    return next();
  },
  function(req, res, next) {
    res.send(req.someData);
    return next();
  }
);

In a restify server, there are three distinct handler chains: 在restify 的服务器中,有三个不同的处理程序链:

pre - a handler chain executed prior to routing pre-在路由之前执行的处理程序链

use - a handler chain executed post routing use-执行后路由的处理程序链

{httpVerb} - a handler chain executed specific to a route {httpVerb} - 对于特定路由执行的处理程序链

All three handler chains accept either a single function, multiple functions, or an array of functions. 所有三个处理程序链都可以接受单个函数,多个函数或一组函数。

Universal pre-handlers: server.pre()

Christian-health commented 7 years ago

https://restify.github.io/docs/home/