我正在尝试检查内容的类型,但它似乎不起作用。这是外壳上的输出:
$ 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 脚本的内容:
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
Accept 标头触发req.is('html')
函数,甚至application/xhtml+xml
Accept 标头触发req.is('xml')
,但该else
语句实际上会运行。我正在使用 Express 3.0.0 rc1 和 Node 0.8.1。