Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Update eject example to something more robust

未关闭
#616 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
领域
cli, documentation

调研方向

该 issue 没有指出 eject 模板文件或测试;首先在 fastify-cli 中定位 eject 示例,并检查生成的应用如何注册插件和启动服务器。更新示例,使其在开始监听之前验证注册,并展示 .ready();当生成的示例反映这一流程且相关检查通过时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Prerequisites
  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal

Change the eject file example to validate everything before running the server and make use of the .ready() function.

Motivation

What made me think of this approach are:

  • Fastify encapsulation concept, the server should not run if something goes wrong with plugins and are not loaded inside correctly inside Fastify.
  • The struggle I've been in the past few days trying to use Fastify in a complex real-world project and I didn't find clear examples.
  • easier to handle errors, if something goes wrong Fastify will throw the valid error not some error buried deep inside the router or hooks
  • the unclear error "cannot read properties of undefined (reading 'length')" which was happening for several reasons and I cannot detect them until i throw error on startup
Example
// Read the .env file.
import * as dotenv from "dotenv";
dotenv.config();

// Require the framework
import Fastify from "fastify";

// Require library to exit fastify process, gracefully (if possible)
import closeWithGrace from "close-with-grace";

// Instantiate Fastify with some config
const app = Fastify({
  logger: true,
});
// make use of the `.ready()` to prevent going on race condition
fastify.register(app).ready((err) => {
   // throw error if something happened on registering `plugins` or `routes`
	if (err) throw err;
	// delay is the number of milliseconds for the graceful close to finish
	const closeListeners = closeWithGrace(
		{
			delay:
				parseInt(process.env.FASTIFY_CLOSE_GRACE_DELAY as string) ||
				500,
		},
		async function ({ signal, err, manual }) {
			if (err) {
				fastify.log.error(err);
			}
			await fastify.close();
		} as closeWithGrace.CloseWithGraceAsyncCallback
	);
	fastify.addHook("onClose", async (instance, done) => {
		closeListeners.uninstall();
		done();
	});
	//server listen
	const port = process.env.PORT || 8000;
	// Start listening.
	fastify.listen({ port: parseInt(port as string) }, (err: any) => {
		if (err) {
			fastify.log.error(err);
			process.exit(1);
		}
	});
});
主要语言
JavaScript
星标
733
派生
179
平均合并
8 天 12 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fastify/fastify-cli 的其他 Issue

查看 fastify/fastify-cli 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。