在官方Restify repo中也被问及:#1224
嗨,
有没有可能有一个默认的格式化程序,可以处理任何未定义的接受类型。
例如:
restify.createServer({
formatters: {
'application/json': () => {},
// All other requests that come in are handled by this, instead of throwing error
'application/every-thing-else': () => {}
}
});发布于 2016-11-14 21:46:58
从表面上看,这是不可能的。由于格式化程序存储在字典中,因此无法创建与每个输入匹配的键(无论如何,这都会让字典失去意义……)在JSON之外完成这类工作的唯一方法是使用正则表达式,而正则表达式不适用于JSON。
这是我写的一个程序来测试它。
var restify = require("restify");
var server = restify.createServer({
formatters: {
'application/json': () => { console.log("JSON") },
"[\w\W]*": () => { console.log("Everything else") } // Does not work
}
});
server.get("/", (req, res, next) => {
console.log("Root");
res.setHeader("Content-Type", "not/supported");
res.send(200, {"message": "this is a test"});
next()
});
server.listen(10000);这里还有一个文档的链接,以防你能找到一些我看不到的提示。Restify documentation
https://stackoverflow.com/questions/40475723
复制相似问题