2017-07-28 154 views
1

我有要求在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 -v http://localhost:3000/我看到一個自定義HTTP標頭abc=123,但HTTP狀態消息仍然OK,而不是預期的Hello world。請幫忙。 謝謝。

回答

2

這是一個錯誤,有一個打開的pull request來解決它。 我可以確認我能夠用Hapi 16.5(來自主人)重現您的問題,並且在應用拉取請求之後,它可以正常工作。

如果你需要它,現在,不能等待拉入請求被接受,你可以跟着我接過來測試它的步驟:

$ 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 

現在你的自定義狀態消息應該工作:)

+1

拉請求合併到主分支 –

+0

我更新了我的項目依賴文件'package.json',像這樣:''hapi「:」^ 16.5.2「',來自項目文件夾的「node_modules」,發出'npm i'命令,因此node_modules重載了正確的hapijs版本。但我仍然得到了響應:'HTTP/1.1 200 OK',而不是預期的'HTTP/1.1 200 Hello world' – lospejos