首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restify:设置默认格式设置

Restify:设置默认格式设置
EN

Stack Overflow用户
提问于 2016-11-08 06:20:29
回答 1查看 414关注 0票数 3

在官方Restify repo中也被问及:#1224

嗨,

有没有可能有一个默认的格式化程序,可以处理任何未定义的接受类型。

例如:

代码语言:javascript
复制
restify.createServer({
    formatters: {
        'application/json': () => {},
        // All other requests that come in are handled by this, instead of throwing error
        'application/every-thing-else': () => {}
    }
});
EN

回答 1

Stack Overflow用户

发布于 2016-11-14 21:46:58

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

这是我写的一个程序来测试它。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40475723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档