Is it possible to call a fetch in JS to a custom port?
- User
Is it possible to call a fetch in JS to a custom port?
Assistant
Yes, you can absolutely call fetch() to a custom port in JavaScript. You just need to include the port number in the URL after the hostname, separated by a colon.
Here are some examples:
// Fetch from localhost on port 3000 fetch('http://localhost:3000/api/data') .then(response => response.json()) .then(data => console.log(data)); // Fetch from a remote server on port 8080 fetch('https://example.com:8080/api/users') .then(response => response.json()) .then(data => console.log(data)); // Using a variable for the port const port = 5000; fetch(`http://localhost:${port}/api/endpoint`) .then(response => response.json()) .then(data => console.log(data));A few things to keep in mind:
-
If you're making requests from a web page to a different port (even on the same domain), you may encounter CORS (Cross-Origin Resource Sharing) issues. The server needs to be configured to allow cross-origin requests.
-
Make sure the server is actually listening on the port you're trying to connect to.
-
For HTTPS connections, the default port is 443, and for HTTP it's 80. If you're using these standard ports, you don't need to specify them explicitly.
-
Some ports may be blocked by firewalls or browser security policies, especially ports below 1024 (which are considered privileged ports).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.