CORS / Helmet
info
Use AdditionalRequest to share data between hooks and controller.
Examples
- Fastify
- Express
- npm
- yarn
cd server
npm install @fastify/cors @fastify/helmet
cd server
yarn add @fastify/cors @fastify/helmet
server/service/app.ts
import Fastify, { FastifyServerFactory } from 'fastify';
import helmet from '@fastify/helmet';
import cors from '@fastify/cors';
import { 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);
server(app, { basePath: API_BASE_PATH });
return app;
};
- expressjs/cors: Node.js CORS middleware
- helmetjs/helmet: Help secure Express apps with various HTTP headers
- npm
- yarn
cd server
npm install cors helmet
npm install -D @types/cors
cd server
yarn add cors helmet
yarn add -D @types/cors
server/entrypoints/index.ts
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import server from '$/$server';
import { API_BASE_PATH } from '$/service/envValues';
export const init = () => {
const app = express();
app.use(helmet());
app.use(cors());
server(app, { basePath: API_BASE_PATH });
return app;
};