Express.js检查请求类型与.is()不工作 [英] Express.js checking request type with .is() doesn't work
问题描述
我正在尝试检查内容的类型,但它似乎不起作用。这是shell的输出:
I'm trying to check what the type of content is, but it doesn't appear to be working. Here is the output on the shell:
$ node app.js
# Request http://localhost:3000 via browser
CLIENT DOES NOT WANT XML, JSON, OR HTML
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
这里是app.js脚本的内容:
And here is the contents of the app.js script:
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res) {
if (req.is('xml')) {
console.log("CLIENT WANTS XML");
} else if (req.is('json') || req.is('html')) {
console.log("CLIENT WANTS JSON OR HTML");
} else {
console.log("CLIENT DOES NOT WANT XML, JSON, OR HTML");
console.log(req.header('Accept'));
}
});
app.listen(3000);
我希望 text / html
接受标题来触发 req.is('html')
函数,甚至 application / xhtml + xml
接受标题触发 req.is('xml')
,但 else
语句实际上可以运行。我正在使用Express 3.0.0 rc1和Node 0.8.1。
I would expect the text/html
Accept header to trigger the req.is('html')
function or even the application/xhtml+xml
Accept header to trigger the req.is('xml')
, but the else
statement actually gets run. I'm using Express 3.0.0 rc1 and Node 0.8.1.
推荐答案
原来我应该使用 .accepts()
,而不是 .is()
。
Turns out I should be using .accepts()
, instead of .is()
.
这篇关于Express.js检查请求类型与.is()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!