[Hapi.js] Replying to Requests
hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serialize objects, buffers, promises and even streams. This post will demonstrate first hand how to use the reply method to send your data to the client, no matter the format.
'use strict'
const Hapi = require('hapi')
const Boom = require('boom')
const server = new Hapi.Server()
server.connection({ port: 8000 }) server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
reply() // When called without arguments, Hapi responds with a 200 and empty payload.
// reply(null, 'hello world') // Reply will inspect the first arguments type, only responding with an error if the first argument is in fact an error. Otherwise, it assumes that it should be the response payload. This is not an error
// reply('hello world')
// reply({ hello: 'world' })
// reply(Promise.resolve('hello world'))
// reply(require('fs').createReadStream(__filename))
// reply(new Error('oops')) //If I pass an error object to reply, Hapi will respond to the client with a 500 error.
// reply(Boom.notFound())
}
}) server.start(() => {})
[Hapi.js] Replying to Requests的更多相关文章
- [Hapi.js] Extending the request with lifecycle events
Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- [Hapi.js] Logging with good and good-console
hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [Hapi.js] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [Hapi.js] Friendly error pages with extension events
hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [Hapi.js] Serving static files
hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...
随机推荐
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the ...
- p标签里面不要放div标签(块元素)
最好不要在p标签里面嵌套块级元素(如div Ul): <p>我来测试下<div>块元素</div>放在p标签的情况</p> <p>我来测试下 ...
- 解决 innerHTML 在 IE6-IE9中不能赋值的bug
在MSDN可以了解跟多,关于innerHTML的介绍,但是在这里只要是解决表格部分问题 MSDN上有这样的记录: When using innerHTML to insert script, you ...
- 2.IKAnalyzer 中文分词器配置和使用
一.配置 IKAnalyzer 中文分词器配置,简单,超简单. IKAnalyzer 中文分词器下载,注意版本问题,貌似出现向下不兼容的问题,solr的客户端界面Logging会提示错误. 给出我配置 ...
- oracle 数据库 if...elsif...语句
CREATE OR REPLACE FUNCTION "UFN_GETIDS" ( OPEKIND IN VARCHAR2,-- 查询类型 PARAMS IN ...
- [Mugeda HTML5技术教程之17] 理解Mugeda访问统计结果
1. 功能简介 Mugeda提供动画统计功能,使得动画制作者可以直观的了解动画的浏览情况,包括浏览量,参与度,以及观看者的分布情况. 目前统计功能主要展示动画内容和广告工程的统计数据.在动画被发布或导 ...
- git merge –squash介绍
Git相对于CVS和SVN的一大好处就是merge非常方便,只要指出branch的名字就好了,如: $ git merge another $ git checkout another # modif ...
- Python 学习日记(第二周)
从这周开始我就正式学习Python 语言了.以后每周都会有一篇有关于学习Python的见闻与大家分享! Python的安装 学习的第一步首先要有一个运行的环境.所以接下来介绍一下安装的步骤. 通过Py ...
- pyqt5模块