[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 ...
随机推荐
- Javascript高级程序设计读书笔记(第六章)
第6章 面向对象的程序设计 6.2 创建对象 创建某个类的实例,必须使用new操作符调用构造函数会经历以下四个步骤: 创建一个新对象: 将构造函数的作用域赋给新对象: 执行构造函数中的代码: 返回新 ...
- GDI+(Graphics Device Interface)例子
使用SolidBrush 单色画笔 Bitmap bitmap = new Bitmap(800, 600); Graphics graphics = Graphics.From ...
- linq读书笔记1-linq 初步
至于linq是什么之类的已经有过太多的文章介绍,亦不清楚的胡朋友可以自己搜索一下便可以得到大量的答案 为了体验linq究竟能带给我们什么体验,我们直接从代码入手: string[] words = n ...
- kafka的一些常用命令
启动zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties & 启动kafka bin/kafka-server ...
- 使用QTP打开应用程序的三种方法
1. systemUtil.Run ‘SystemUtil对象的Run方法 SystemUtil.Run “http://192.168.11.82/XXX” 参数实例: File:“http://1 ...
- txt 分割程序
网上有很多 分割程序 ,但是他们都没有满足实际的用户要求 ,大家当然是希望看文章小说 一章节一章节的看 并非是那些传统意义上的按照文件的大小切割 所以 我特写本文研究下 这个简单的算法该怎样设计 说白 ...
- 外观模式-facade实现interface的方式(简单工厂+facade组合使用)
Façade 外观模式 1.Façade实现为interface的具体过程 在Façade.java 接口 工厂 将构造方法私有 static 方法产生一个工厂 此时 客户端不知道 Façade的存 ...
- 寻找子串位置 codevs 1204
题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...
- myeclipse10 如何把代码预览的窗口去掉
1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...
- jquery height、innerHeight、outHeight
JQuery有很多的height,不总结一下你就要被他搞晕,所以为了保持清醒,汇总在下面 height:height innerHeight:height+padding outerHeight(fa ...