Skip to content
TheChatArchiveThe Chat Archive
Hunyuanhunyuan-turbos-20250416· TencentHugging Face LMArena Human Preference 140K

Is it possible to call a fetch in JS to a custom port?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514’s answer.
  1. User

    Is it possible to call a fetch in JS to a custom port?

  2. Hunyuan Assistant

    Yes, it is absolutely possible to make a fetch request in JavaScript to a server running on a custom port. The fetch API allows you to specify the full URL—including the protocol, hostname, port, and path—as long as the port is open and accessible from the client.

    Example

    fetch('http://localhost:3000/api/data')
      .then(response => {
        if (!response.ok) {
          throw new Error(`HTTP error! Status: ${response.status}`);
        }
        return response.json();
      })
      .then(data => {
        console.log('Data received:', data);
      })
      .catch(error => {
        console.error('Fetch error:', error);
      });
    

    In this example:

    • localhost is the host.
    • 3000 is the custom port.
    • /api/data is the endpoint path.

    Important Notes:

    1. Same-Origin Policy (CORS):

      • If your frontend JavaScript is served from a different origin (different protocol, domain, or port) than your backend server, you must handle Cross-Origin Resource Sharing (CORS) on the server.
      • Otherwise, the browser will block the request due to security restrictions.
    2. Secure Contexts (HTTPS):

      • If your page is served over HTTPS, calling a non-secure endpoint (http://...) might be blocked depending on browser settings.
    3. Firewalls / Network Restrictions:

      • Ensure that the custom port is open on the server and not blocked by firewalls or corporate network policies.
    4. Browser Support:

      • fetch() is supported in all modern browsers. For older browsers (like IE), you'd need a polyfill like whatwg-fetch.

    CORS Example (Server-Side with Express.js):

    If you're using Node.js/Express, you can enable CORS for all routes like this:

    const express = require('express');
    const cors = require('cors');
    const app = express();
    
    app.use(cors()); // Enable CORS for all requests
    
    app.get('/api/data', (req, res) => {
      res.json({ message: 'Hello from custom port!' });
    });
    
    app.listen(3000, () => {
      console.log('Server listening on port 3000');
    });
    

    ✅ So yes — just use the full URL with the desired port in your fetch() call, and ensure your backend is configured to accept cross-origin requests if needed.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.