跳到主要内容

Fastify 集成指南

Anima 提供了针对 Fastify 框架的原生集成方案,让您无需手动管理隧道,开发服务器启动时即可自动开启公网访问。

安装

npm install @anima/fastify

使用

在您的 Fastify 应用中,通过插件方式集成 Anima:

const fastify = require('fastify')({ logger: true });
const anima = require('@anima/fastify');

// 在开发环境自动启用隧道
if (process.env.NODE_ENV === 'development') {
fastify.register(anima, {
token: process.env.ANIMA_TOKEN,
subdomain: 'my-fastify-app',
autoStart: true,
onReady: (url) => {
console.log(`🚀 Anima 隧道已就绪: ${url}`);
}
});
}

fastify.get('/', async (request, reply) => {
return { hello: 'from Fastify!' };
});

const start = async () => {
try {
await fastify.listen({ port: 3000 });
console.log(`✓ Fastify 服务器启动: http://localhost:3000`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();

示例项目

查看完整的 Fastify 示例项目:GitHub 仓库链接