Restify:设置默认格式化程序



在官方retify 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': () => {}
    }
});

从表面上看,这是不可能的。由于格式化程序存储在字典中,因此无法创建与每个输入匹配的键(这将破坏字典的意义……)在JSON之外完成这类事情的唯一方法是使用正则表达式,而正则表达式不适用于JSON。

我写了一个程序来测试这个

var restify = require("restify");
var server = restify.createServer({
    formatters: {
        'application/json': () => { console.log("JSON") },
        "[wW]*": () => { 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文档

最新更新