gpt4 book ai didi

Restify:设置默认格式化程序

转载 作者:行者123 更新时间:2023-12-01 15:51:49 24 4
gpt4 key购买 nike

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

最佳答案

从表面上看,这是不可能的。由于格式化程序存储在字典中,因此无法创建匹配每个输入的键(无论如何这都会破坏字典的意义......)在 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

关于Restify:设置默认格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com