[Hapi.js] Replying to Requests

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 上的路径上使用 hapi-auth-jwt2 身份验证?

hapi.js - 404 路由 VS 静态文件路由

如何使用 dropzone.js 和 hapi.js 上传图片

Hapi.js —— Node.js 服务器端应用开发框架

“502 Bad Gateway”将 hapi.js 部署到 AWS Beanstalk?

如何使用hapi.js读取已在浏览器中设置的cookie