Listening on localhost is a security nightmare, also because it is accessible from other user accounts on the same machine. The server probably can do some privileged tasks (otherwise you wouldn’t need it) and could be hijacked by malware.
And there are numerous alternatives to Electron if you don't like to ship a full Chrome browser. It get's a bit more complicated then, but its possible, take a look at Tauri for example.
To be honest, I had multiple projects, where a http://localhost solution was proposed, and we found a better solution without it. It is very rarely the best thing to do.
If you use http://localhost, you need to authenticate both server and client. It can be done, but often it is not done properly. This authentication needs to be present on every request. And that’s tricky, because you would need to track every http-tcp-connection, and ask the server for authentication in the beginning of the connection. Once you open a TCP new connection, you would need to do that again. As far as I know, you can’t do that with JavaScript inside a browser.
Another preventable mistake is, that the server somehow gets accessible not only from localhost, but gets exposed to a public network interface somehow.
So all in all a lot of possible headaches, that can be prevented by choosing a different architecture.
For about 5 years it is possible to call localhost from https page without any warning. Localhost is a secure context, exactly that you don't need to do this.
I cannot find the docs that says that, but... it is true. You can try that yourself.
W3C has it here https://w3c.github.io/webappsec-secure-contexts/#localhost
you can see in Chrome source code let's say here
https://source.chromium.org/chromium/chromium/src/+/main:thi...