zoukankan      html  css  js  c++  java
  • [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(() => {})
  • 相关阅读:
    [HNOI2004]L语言
    快速沃尔什变换FWT
    [BZOJ1486][HNOI2009]最小圈
    [BZOJ4819][SDOI2017]新生舞会
    [POJ2976]Dropping tests
    CTSC2018&APIO2018游记
    [Luogu3769][CH弱省胡策R2]TATT
    [BZOJ3489]A simple rmq problem
    [BZOJ4066]简单题
    [BZOJ2648]SJY摆棋子
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5223994.html
Copyright © 2011-2022 走看看