[Hapi.js] Using the response object

When you use reply method:

let resp = reply(‘hello world‘)

It actually return an response object.

By using response object, you can modiy the response‘s status code, header, cookie, type and so on...

server.route({
  method: ‘GET‘,
  path: ‘/‘,
  handler: function(request, reply) {
    let resp = reply(‘hello world‘)
    resp.code(418) // set status code to 418
    resp.type(‘text/plain‘) // set type to text/plain
    resp.header(‘hello‘, ‘world‘) // set header to hello: world
    resp.state(‘hello‘, ‘world‘) // set cookie to hello=world
  }

response object is chainable:

server.route({
   method: ‘GET‘,
   path: ‘/‘,
   handler: function(req, reply){
      reply(‘hello world‘)
        .code(418)
        .type(‘text/plain‘)
        .header(‘hello‘, ‘world‘)
       -state(‘hello‘, ‘world‘)
   }
})  
时间: 2024-10-12 01:20:17

[Hapi.js] Using the response object的相关文章

每天一个JavaScript实例-从js脚本中访问object元素中的SVG

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-从js脚本中访问object元素中的SVG</title> <style> </style> </head> &l

[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

[Hapi.js] Request Validation with Joi

hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi's simple, 'use strict' const Hapi = require('hapi') const Joi = require('joi') const server

[Hapi.js] Managing State with Cookies

hapi has built-in support for parsing cookies from a request headers, and writing cookies to a response, making state management easy and straight-forward. It even has built in support for cookie encryption and auto detects when a cookie contains JSO

[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 your application needs errors rendered in HTML? This lesson shows how to implement friendly HTML error messages using hapi extension events. const Hapi

[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 you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension even

[Hapi.js] View engines

View engines, or template engines, allow you to maintain a clean separation between your presentation layer and the rest of your application. This post will demonstrate how to use the vision plugin with hapi to enable template support. index.js serve

[Hapi.js] Logging with good and good-console

hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes everything needed to configure logging for your application. This post will introduce good, a process monitor for hapi, and good-console, a reporter for g

js如何获取response header信息

信息转自网上 普通的请求JS无法获取,只有ajax请求才能获取到. $.ajax({ type: 'HEAD', // 获取头信息,type=HEAD即可 url : window.location.href, complete: function( xhr,data ){ // 获取相关Http Response header var wpoInfo = { // 服务器端时间 "date" : xhr.getResponseHeader('Date'), // 如果开启了gzip,