diff --git a/src/endpoints/CompactRecipePoint.ts b/src/endpoints/CompactRecipePoint.ts index 0df0e0a..e7c6956 100644 --- a/src/endpoints/CompactRecipePoint.ts +++ b/src/endpoints/CompactRecipePoint.ts @@ -22,6 +22,7 @@ router.get( asyncHandler(async (req, res) => { // extract search string from query parameters, convert to lower case for case insensitive search const searchString : string = req.query.search ? req.query.search.toString().toLowerCase() : ""; + console.log("Searching for recipes with title containing", searchString) const response = await compactRecipeHandler.getMatchingRecipes(searchString); res.status(201).json(response); }) diff --git a/src/index.ts b/src/index.ts index 73cf6fb..d0d06fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,9 +27,24 @@ async function startServer() { await AppDataSource.runMigrations(); console.log("Migrations executed"); + // Enable CORS before anything else + // @todo move to middleware util + app.use((req: Request, res: Response, next: NextFunction) => { + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'); + res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + + // Handle preflight requests quickly + if (req.method === 'OPTIONS') { + return res.sendStatus(200); + } + + next(); + }); + // Activate Authentication app.use(authentication); - + // Setup routes app.use(authBasicRoute, authRoutes); app.use("/user", userRoutes); diff --git a/src/repositories/RecipeRepository.ts b/src/repositories/RecipeRepository.ts index 493b7f0..8c62cb2 100644 --- a/src/repositories/RecipeRepository.ts +++ b/src/repositories/RecipeRepository.ts @@ -1,7 +1,7 @@ import { AbstractRepository } from "./AbstractRepository.js"; import { RecipeEntity } from "../entities/RecipeEntity.js"; import { AppDataSource } from "../data-source.js"; -import { Like } from "typeorm"; +import { ILike, Like } from "typeorm"; export class RecipeRepository extends AbstractRepository { constructor() { @@ -32,7 +32,7 @@ export class RecipeRepository extends AbstractRepository { async findCompactRecipeBySearch(searchString : string): Promise{ // @todo doesn't work like expected... return this.repo.find( - { where: {title: Like(`%${searchString}%`)}} + { where: {title: ILike(`%${searchString}%`)}} ); }