响应 API
目录
Response
拓展 http.ServerResponse
封装了所有的 Node.js http.ServerResponse API、事件和属性,以及以下内容。
cache
设置 cache-control 头。
参数
typeString 报头的值("public"or"private")(可选,默认为"public")optionsObject? 一个选项对象options.maxAgeNumber max-age(秒)
返回 String 设置为报头的值。
noCache
关闭所有与缓存相关的报头。
返回 Response 自身,响应对象。
charSet
将提供的字符集附加到响应的 Content-Type 上。
参数
typeString 字符集的值
例子
res.charSet('utf-8');
返回 Response 自身,响应对象。
header
设置响应的报头。
参数
例子
如果仅指定了关键字,则返回报头的值。如果指定了键和值,则设置响应头。
res.header('Content-Length');// => undefinedres.header('Content-Length', 123);// => 123res.header('Content-Length');// => 123res.header('foo', new Date());// => Fri, 03 Feb 2012 20:09:58 GMT
在合适的情况下,header() 也可以用来自动链接报头的值:
res.header('x-foo', 'a');res.header('x-foo', 'b');// => { 'x-foo': ['a', 'b'] }
返回 Object 检索或设置的值。
json
语法糖:
res.contentType = 'json';res.send({hello: 'world'});
参数
例子
res.header('content-type', 'json');res.send({hello: 'world'});
返回 Object 响应对象。
link
设置链接报头。
参数
返回 String 将报头的值设置为资源对象。
send
发送响应对象。传递给基于 content-type 头的格式化程序使用的内部 __send。
参数
例子
您可以使用 send() 在 Node.js 的 HTTP API 上封装所有常用的 writeHead()、write() 和 end() 调用。
您可以传 code 和 body,也可以只传一个 body。body可以时一个 Object、Buffer 或 Error。
当您调用 send() 时,restify 会根据 content-type 指出如何格式化响应。
res.send({hello: 'world'});res.send(201, {hello: 'world'});res.send(new BadRequestError('meh'));
返回 Object 响应对象。
sendRaw
和 res.send() 一样,但跳过了格式化这一步。当载体内容已经被预先格式化时,这会非常有用。
发送响应对象。完全跳过格式化程序并将原始内容传递给内部的 __send。
参数
返回 Object 响应对象。
set
在响应中设置多个报头。
内部使用 header(),启用多个值的报头。
参数
例子
res.header('x-foo', 'a');res.set({'x-foo', 'b','content-type': 'application/json'});// =>// {// 'x-foo': [ 'a', 'b' ],// 'content-type': 'application/json'// }
返回 Object 自身,响应对象。
status
设置响应中的 http 状态码。
参数
codeNumber http 状态码
例子
res.status(201);
返回 Number 传入的状态码。
redirect
Redirect 是重定向的语法糖。
参数
optionsObject 用来配置重定向的链接或一个选项对象options.secureBoolean? 是否重定向到 http 或 httpsoptions.hostnameString? 重定向位置的主机名options.pathnameString? 重定向位置的路径名options.portString? 重定向位置的端口号options.queryString? 重定向位置的查询字符串参数options.overrideQueryBoolean? 如果为true,options.query会舍弃当前 URL 上的任何现有的查询参数。默认情况下,会合并这两个值。options.permanentBoolean? 如果为true,设置为 301。默认为 302。
nextFunction 强制性,完成响应并触发审计记录器。
例子
res.redirect({...}, next);
301/302 重定向的便捷方法。使用该方法会告诉 restify 停止执行您的处理程序链。
您也可以使用选项对象。必须要有 next:
res.redirect({hostname: 'www.foo.com',pathname: '/bar',port: 80, // 默认为 80secure: true, // 设置 httpspermanent: true,query: {a: 1}}, next); // => 重定向到 301 https://www.foo.com/bar?a=1
返回 undefined
redirect
使用状态码和网址进行重定向。
参数
例子
res.redirect(301, 'www.foo.com', next);
返回 undefined
redirect
通过网址重定向。
参数
例子
res.redirect('www.foo.com', next);res.redirect('/foo', next);
返回 undefined
