I’m building a custom web frontend for configuring router os devices using their new rest API.
Is it somehow possible to set CORS headers for the www-ssl to allow HTTP requests directly from a custom-built website without an intermediate backend?
Yup. It really open some possibilities. “Wasteful” is a good term.
I have long wanted to include a static .html page in a branding kit, with some JS to directly invoke RouterOS from the page – no backend required, no container, and everything be local to CPE. But the lack of even a CORS “same-origin” mean some backend and/or CORS reverse proxy is required.
I did put in a formal feature request (SUP-99112) a while back, and got a “we will see what can be done here” a while back – so semi-positive news. Specifically I purposed have a checkbox to enable in /ip/services with along with list FQDNs for the “allowed origins” returned by REST API to maintain CORS’s schemes*. On client-side, still have to the CORS dance (e.g. includeCredentials: true etc in fetch()/XMLRequest/etc) - but those are solvable. The lack of CORS headers being returned from REST server is not.
* there are many CORS rules on client side, so cors-allowed-origin header cannot be “*” when using credentials & RouterOS requires creds (e.g. basic auth)…so REST API have to deal with the origins somehow
The choice of client-side framework doesn’t change how a web browsers enforce CORS. So unless Mikrotik adds CORS support, there is no way for client-side JavaScript to directly use fetch() or XMLHttpRequest() — React framework nor packaging tools can not change it.
Now nodejs does NOT enforce CORS rules. And Vite is just a development nodejs server, so that’s an option. But any backend will do (e.g. Express, Hapi, etc. or some serverless lambda). Key is the rest api calls to RouterOS happen in the nodejs backend, not the front-end JS/HTML – but the frontend must use that “intermediate backend” (e.g. a nodejs-based HTTP server) to reach the router REST API.