restify Server API
来源:互联网 发布:马里亚纳网络有什么 编辑:程序博客网 时间:2024/05/07 19:29
Server API
原文:http://mcavage.me/node-restify/#server-api
最准系统响应服务器:
var restify = require('restify');function respond(req, res, next) { res.send('hello ' + req.params.name); next();}var server = restify.createServer();server.get('/hello/:name', respond);server.head('/hello/:name', respond);server.listen(8080, function() { console.log('%s listening at %s', server.name, server.url);});
试着打用下面的 curl 命令来感受RESTify将会把它转换成什么:
$ curl -is http://localhost:8080/hello/mark -H 'accept: text/plain'HTTP/1.1 200 OKContent-Type: text/plainContent-Length: 10Date: Mon, 31 Dec 2012 01:32:44 GMTConnection: keep-alivehello mark$ curl -is http://localhost:8080/hello/markHTTP/1.1 200 OKContent-Type: application/jsonContent-Length: 12Date: Mon, 31 Dec 2012 01:33:33 GMTConnection: keep-alive"hello mark"$ curl -is http://localhost:8080/hello/mark -X HEAD -H 'connection: close'HTTP/1.1 200 OKContent-Type: application/jsonContent-Length: 12Date: Mon, 31 Dec 2012 01:42:07 GMTConnection: close
请注意,在默认情况下,curl 使用 Connection:keep-alive。为了使头部的方法立即返回,你就需要通过 Connection:close。
由于curl 通常与REST API一起使用,RESTify提供一个插件来解决围绕这个特质的 curl。该插件检查是否用户代理是curl。如果是,它设置连接头为“close”,并删除“的Content-Length”头。
server.pre(restify.pre.userAgentConnection());
看 pre 方法,以获取更多信息。
创建一个服务
创建服务器很简单,你只需调用createServer API,它带有一个对象选项,下面列出的选项(并且 listen() 采用相同的参数作为node 的 http.Server.listen):
var restify = require('restify');var server = restify.createServer({ certificate: ..., key: ..., name: 'MyApp',});server.listen(8080);
0 0
- restify Server API
- Restify
- Restify实践
- 关于 restify
- restify Routing
- Restify一
- [菜鸟试水]关于Nodejs搭建后台API服务(Mysql-Restify)
- 【个人笔记重点,不作为参考】主题:restify搭建模拟RESTful API
- Restify中文编码问题
- MMORPG Server - API
- ArcGIS Server Flex API
- kubernetes API Server安全
- 使用Restify+superagent做数据转发
- 使用restify构建纯粹的rest服务
- arcgis server api选择困惑
- RESTFUL API Server in C++
- kubernetes 简介:API Server 简介
- kubernetes 简介:API Server 简介
- 齐次坐标
- 大数模板
- 【DirectX 游戏开发基础】自制游戏微型引擎
- Android 通过Uri获取Bitmap对象
- netty源码分析
- restify Server API
- 处理器分类
- 项目实战No3 推荐标签
- Linux统计某文件夹下文件、文件夹的个数
- 剑指offer:旋转数组的最小数字
- Codeforces Round #318 D. Bear and Blocks
- php引用外部图片如何写
- 欢迎使用CSDN-markdown编辑器
- 文本编辑三剑客之sed