メインコンテンツまでスキップ

カスタムエントリーポイント

Fastify や Express のインスタンスは server/service/app.ts で作られ、server/entrypoint/index.ts で起動されます。

  • $/service/app.ts : プラグインを登録するためにサーバーインスタンスにアクセスする
  • $/entrypoint/index.ts : listen するアドレスやポートを変更する

備考

以下のコードは create-frourio-app で生成されたものに変更を加えています。

$/service/app.ts
import Fastify, { FastifyServerFactory } from 'fastify';
import helmet from '@fastify/helmet';
import cors from '@fastify/cors';
import fastifyJwt from '@fastify/jwt';
import { API_JWT_SECRET, API_BASE_PATH } from '$/service/envValues';
import server from '$/$server';

export const init = (serverFactory?: FastifyServerFactory) => {
const app = Fastify({ serverFactory });
app.register(helmet);
app.register(cors);
app.register(fastifyJwt, { secret: API_JWT_SECRET });
server(app, { basePath: API_BASE_PATH });
return app;
};
$/entrypoints/index.ts
import { init } from '$/service/app';
import { API_SERVER_PORT } from '$/service/envValues';

init().listen(API_SERVER_PORT, '0.0.0.0');