@ fastify/ 路径
此插件将快捷实例装饰为 routes,这是 地图 登记路线。 请注意, 您必须等待此插件的注册后才能注册任何路径, 这样 @ fastify/ routes 就可以收集它们 .
数据结构
这个 fastify.routes 地图对每一个路径都有一个密钥 任何已经注册的路径都指在该路径上注册的一系列路径. 如果增加了多个路线,但方法不同或限制不同,则给定的路径可能不止一条.
{
'/hello': [
{
method: 'GET',
url: '/hello',
schema: { ... },
handler: Function,
prefix: String,
logLevel: String,
bodyLimit: Number,
constraints: undefined,
},
{
method: 'POST',
url: '/hello',
schema: { ... },
handler: Function,
prefix: String,
logLevel: String,
bodyLimit: Number,
constraints: { ... },
}
]
}
示例
const fastify = require("fastify")();
(async () => {
await fastify.register(require("@fastify/routes"));
fastify.get("/hello", {}, (request, reply) => {
reply.send({ hello: "world" });
});
fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err);
return;
}
console.log(fastify.routes);
/* will output a Map with entries:
{
'/hello': [
{
method: 'GET',
url: '/hello',
schema: Object,
handler: <Function>,
prefix: <String>,
logLevel: <String>,
bodyLimit: <Number>
}
]
}
*/
});
})();
许可证
MIT 许可证