node-restify简介
发布于 12 年前 作者 roymax 28609 次浏览 最后一次编辑是 8 年前

原文链接

restifyNode.js的模块。虽然restify的API或多或少的参考了express,但restify不是一个MVC框架,它是一套为了能够正确构建REST风格API而诞生的框架。

###安装restify

restify安装到目录restify-dmeo

$ mkdir restify-demo
$ cd restify-demo
$ npm install restify

###Hello World

var restify = require('restify');

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

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

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

保存文件并执行

$ node app.js
restify listening at http://0.0.0.0:8080

通过curl命令测试,服务器返回HTTP状态码200和一个application/json类型的内容(事实上这里不是)

$ curl -is http://localhost:8080/hello/roy
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version
Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time
Server: restify
X-Request-Id: ec3e9e55-7083-4e12-93ee-9c24a0c5c398
Access-Control-Allow-Methods: GET
Connection: close
Content-Length: 5
Content-MD5: 65tW24XBT3r1jLUpw0o0Fw==
Content-Type: application/json
Date: Sun, 19 Feb 2012 16:14:13 GMT
X-Response-Time: 3

"hello roy"

###特性 接触到restify是因为内部系统需要一个手机归属地的查询接口,基于nodejsredis的高性能实现,决定快速实现一个,当时也很自然地选择了express作为web层。但express始终是一个Web MVC框架,它包含了一些我不太需要的功能实现(i.e., templating/rendering)。而我只是需要一个更纯粹的API接口而已–接收请求然后返回一个JSON结果集。

后来从推上找到了restify时,我就觉得这才是我想要的:

  1. 路由

    • 基本等同于express/sinatra的路由方式
    • 路由正则表达式支持
    • 版本化支持
  2. 自动选择内容格式进行响应,并可以自定义格式

  3. 内置REST风格的错误处理机制,重载了400和409来解析应答,得到一个包含codemessage的JSON对象。也可以子类化restify.RestError实现自己错误代码和信息。

    已实现的RestError:

    • RestError
    • BadDigestError
    • BadMethodError
    • InternalErrorError
    • InvalidArgumentError
    • InvalidContentError
    • InvalidCredentialsError
    • InvalidHeaderError
    • InvalidVersionError
    • MissingParameterError
    • NotAuthorizedError
    • RequestExpiredError
    • RequestThrottledError
    • ResourceNotFoundError
    • WrongAcceptError
  4. 插件机制

  5. DTrace支持,这个绝对是调式、诊断和性能测量利器

虽然现在restify还是初生婴儿,我还是把上面提到的手机归属地查询接口改用了restify来实现了一个moquery

正如官方描述一样

restify is a smallish framework, similar to `express` for building REST APIs. 
3 回复

我还在用express来处理rest。看来有必要试试这个了。

LZ, node club 的搭建能分享下吗?

为什么我这启动node app.js 打印的是http://[::]:8080

回到顶部