In this case, an external dependency is not required. Instead, set the CORS headers manually in productsController.addProduct
.
Example of manual CORS header manipulation:
function addProduct(request, reply) {
reply.header("Access-Control-Allow-Origin", "*");
reply.header("Access-Control-Allow-Methods", "POST");
// ... more code here ...
}
If you still want to use fastify-cors
, try something like this:
fastify.register((fastify, options, done) => {
fastify.register(require("fastify-cors"), {
origin: "*",
methods: ["POST"]
});
fastify.route({
method: "POST",
url: "/product",
handler: productsController.addProduct
});
done();
});