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的更多相关文章

  1. [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 ...

  2. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  3. [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 ...

  4. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  5. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  6. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  7. [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 ...

  8. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  9. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

随机推荐

  1. [RxJS] Basic DOM Rendering with Subscribe

    While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...

  2. Android-IM架构设计

    ###1. 架构总览 ###2. 模块介绍 ####2.1 协议封装与任务流程 #####1) 协议与任务的封装 a. 协议有协议头(协议头因为格式相同,被抽象出来)和协议体组成,协议有两类:请求协议 ...

  3. 在WIN7系统下用Quartus ii 11.1 NIOS II 11.1 有时候会出现Nios II 的Run as hardware 中报错:Downloading ELF Process failed

    nios工程在编译通过后RUN的过程中出现Error Running Nios II Project: ‘Downloading ELF Process failed’问题原因: 1.nios2 cp ...

  4. jquery选择指定元素之外的所有元素

    最近的项目中有这么一个需求,点击一排图片中的任意一张后底部弹出一个对话框,要求点击任意地方隐藏对话框 这个时候用not()显然是不现实的,用closest()可以实现差不多的功能 <!DOCTY ...

  5. .NET开发人员必须知道的八个网站

    对于不熟悉.NET技术的朋友,需要说明一下,.NET提供了一个平台和一些相应的工具,.NET开发人员可以使用它们来在开发Windows桌面,互联网,甚至是手持移动设备上构建极富交互性的应用.很有可能你 ...

  6. Eclipse搭建Android开发环境(安装ADT,Android4.4.2)(转)

    使用Eclipse做Android开发,需要先在Eclipse上安装ADT(Android Development Tools)插件. 1.安装JDK 1.7 JDK官网http://www.orac ...

  7. 全世界最详细的图形化VMware中linux环境下oracle安装(三)【weber出品必属精品】

    数据库软件和数据库都建好了,基本上可以说完成90%的工作,但是美中不足的就是企业管理器还没有安装好,现在我们就开始安装企业管理器吧. 安装之前我们先将补丁给补上.补丁我们也是采用禁默安装.补丁:p83 ...

  8. struts 标签引用出错

    几句句话概括 1.检查 web.xml  出现错误自己改 配置后filter  jsp-config 2.检查 tld 目录下的东西 是否缺少 3. 将包复制到web的lib目录下后    之后  b ...

  9. onsubmit提交前先验证(验证不通过阻止form提交)

    <form  onsubmit = "return val();"> <input type="submit" value="提交& ...

  10. 4 常量类--Map常量

    public static final HashMap<String, String> ETL_SOURCE_INPUTTYPE_MAP = new HashMap<String, ...