Skip to main content

Custom Entrypoint

The instance of fastify or express is created in server/service/app.ts and is started in server/entrypoint/index.ts.

  • $/service/app.ts : to access the server instance to register plugins
  • $/entrypoint/index.ts : to change the IP address or port to listen.

Example

info

The following codes are modified from the code generated by 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');