hapijs自定义http状态消息
我有要求在hapijs应用程序中设置一个自定义的HTTP状态消息。 这可以怎么做? 我的代码是:
'use strict'; const Hapi = require('hapi'); const server = new Hapi.Server(); server.connection({ port: 3000, host: 'localhost' }); server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply('Hello, world!\n') .header('set-cookie', 'abc=123') .message("Hello world"); } }); server.start((err) => { if (err) { throw err; } console.log(`Server running at: ${server.info.uri}`); });
当我通过curl这样调用它: curl -v http://localhost:3000/
我看到一个自定义http标头abc=123
,但http状态消息仍然OK
而不是预期的Hello world
。 请帮忙。 谢谢。
这是一个错误,并有一个公开的拉动请求来解决它。 我可以确认,我能够用Hapi 16.5(来自主人)重现您的问题,并且在应用拉取请求之后,它可以正常工作。
如果你现在需要它,不能等待拉取请求被接受,你可以按照我testing它的步骤:
$ rm -r node_modules/hapi $ git clone https://github.com/hapijs/hapi.git node_modules/hapi
然后编辑node_modules/hapi/.git/config
这样[remote "origin"]
块的外观如下:
[remote "origin"] url = https://github.com/hapijs/hapi.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
(添加第三行)
$ cd node_modules/hapi $ git fetch origin $ git checkout pr/3560 // optional if you don't want a nested git repo in your project $ rm -r .git
现在您的自定义状态消息应该工作:)