I have an extra cowboy server running in 8383 port in my Phoenix project. From my 4000 port I am trying to call an api to the 8383. But it throws error saying [Error] Preflight response is not successful
. To fix this, how can I use a CORS Plug in the additional cowboy server. My application.ex code is given below:
defmodule MyApp.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
children = [
# Start the Ecto repository
MyApp.Repo,
# Start the Telemetry supervisor
MyAppWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: MyApp.PubSub},
# Start the Endpoint (http/https)
MyAppWeb.Endpoint,
Plug.Cowboy.child_spec(
scheme: :http,
plug: MyAppWeb.TestSocket,
options: [
dispatch: dispatch(),
port: 8383
]
),
# Start a worker by calling: MyApp.Worker.start_link(arg)
# {MyApp.Worker, arg}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
MyAppWeb.Endpoint.config_change(changed, removed)
:ok
end
defp dispatch do
[
{:_,
[
{"/account/:serial", MyAppWeb.TestSocket, []},
{"/api/account/:serial/command", MyAppWeb.TestRouter, []}
]}
]
end
end