使用restify设置content-type标头会导致application/octet-stream

bra*_*ipt 2 node.js restify

我正在努力解决问题,尽管我对Express更加满意,但到目前为止它还非常棒.我正在尝试在响应中设置内容类型标头,如下所示:

server.get('/xml', function(req, res) {
    res.setHeader('content-type', 'application/xml');
    // res.header('content-type', 'application/xml'); // tried this too
    // res.contentType = "application/xml"; // tried this too
    res.send("<root><test>stuff</test></root>");
});
Run Code Online (Sandbox Code Playgroud)

但我得到的回应却是application/octet-stream.

我也试过,res.contentType('application/xml')但实际上扔了一个错误("Object HTTP/1.1 200 OK\ has no method 'contentType'").

在响应上将内容类型标头设置为xml的正确方法是什么?

更新:

当我这样做console.log(res.contentType);时实际输出application/xml.为什么不在响应头中?

卷曲片段:

* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /xml?params=1,2,3 HTTP/1.1
> User-Agent: curl/7.39.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/octet-stream
< Content-Length: 8995
< Date: Mon, 23 Feb 2015 20:20:14 GMT
< Connection: keep-alive
< 
<body goes here>
Run Code Online (Sandbox Code Playgroud)

bra*_*ipt 13

原来这个失败的原因是因为我没有使用Restify响应处理程序发送响应; 它默认为本机Node.js处理程序.

我这样做的地方:

res.send(js2xmlparser("search", obj));
Run Code Online (Sandbox Code Playgroud)

我应该这样做:

res.end(js2xmlparser("search", o));
//  ^ end, not send!
Run Code Online (Sandbox Code Playgroud)