zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[Hapi.js] Using the response object

JS The object Using response
2023-09-14 08:59:20 时间

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')
   }   
})