Skip to content

feat: add HOST environment variable for configurable bind address#360

Open
mjfork wants to merge 3 commits intositeboon:mainfrom
mjfork:feature/host-binding
Open

feat: add HOST environment variable for configurable bind address#360
mjfork wants to merge 3 commits intositeboon:mainfrom
mjfork:feature/host-binding

Conversation

@mjfork
Copy link

@mjfork mjfork commented Jan 31, 2026

Summary

  • Add HOST environment variable to configure which host/IP the servers bind to
  • Default to 0.0.0.0 (all interfaces) for backward compatibility
  • Update both Express server and Vite dev server to respect the HOST setting

Fixes #359

Changes

  • .env.example: Added HOST environment variable with documentation
  • server/index.js: Use HOST env var instead of hardcoded '0.0.0.0'
  • vite.config.js: Added host option to Vite server config

Test plan

  • Set HOST=127.0.0.1 and verify server only listens on localhost
  • Leave HOST unset and verify server listens on all interfaces (default behavior)
  • Verify both backend server and Vite dev server respect the HOST setting

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Server and dev tooling now respect configurable HOST and PORT environment variables, allowing the network interface and port to be changed without modifying code.
    • Local URLs shown in logs and dev proxies normalize a bind-all address to "localhost" for clearer, user-friendly links.

Allow users to specify which host/IP address the servers should bind to
via the HOST environment variable. Defaults to 0.0.0.0 (all interfaces)
to maintain backward compatibility.

This enables users to restrict the server to localhost only (127.0.0.1)
for security purposes or bind to a specific network interface.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 31, 2026

Walkthrough

Adds a configurable HOST environment variable (default 0.0.0.0) and updates server and Vite dev server startup/logging to use that value, mapping 0.0.0.0 to localhost for display/proxy purposes.

Changes

Cohort / File(s) Summary
Environment Example
\.env.example
Added HOST entry with default 0.0.0.0 and comments explaining binding behavior and localhost mapping.
Server
server/index.js
Reads HOST from env (default 0.0.0.0); added DISPLAY_HOST mapping 0.0.0.0localhost; uses HOST for server listen and updates startup URL log to use DISPLAY_HOST.
Vite Dev Server & Proxies
vite.config.js
Reads HOST and PORT from env (defaults 0.0.0.0/3001); computes proxyHost (localhost when binding to 0.0.0.0); exposes server.host and updates /api, /ws, /shell proxy targets to use computed host/port.

Sequence Diagram(s)

sequenceDiagram
    participant Browser
    participant Vite as Vite Dev Server (proxy)
    participant Backend as App Server
    Browser->>Vite: Request (HTTP / WS)
    Vite->>Vite: compute proxyHost from HOST env
    Vite->>Backend: Proxy request to http/wns://{proxyHost}:{PORT}
    Backend-->>Vite: Response
    Vite-->>Browser: Response
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I nibble bytes and hop on logs,
HOST set kindly, no more fogs,
From 0.0.0.0 I peek as 'localhost',
Proxy paths follow where I post,
A carrot for every deployed cog 🥕

🚥 Pre-merge checks | ✅ 5 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a HOST environment variable for configurable bind address, which matches the primary objective of the PR.
Linked Issues check ✅ Passed The PR successfully implements all three coding requirements from issue #359: adds HOST to .env.example with documentation, updates server/index.js to use HOST, and updates vite.config.js to support HOST binding.
Out of Scope Changes check ✅ Passed All changes are directly related to the HOST environment variable feature. The modifications to .env.example, server/index.js, and vite.config.js align with the stated objectives and no extraneous changes are present.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
vite.config.js (1)

11-22: ⚠️ Potential issue | 🟠 Major

Proxy targets ignore HOST and can break non-local binds.
If HOST is set to a specific interface (e.g., 192.168.1.100), the backend binds only there, but the Vite proxy still targets localhost, which will fail. Use a derived proxyHost that falls back to localhost only when HOST=0.0.0.0.

🐛 Proposed fix
 export default defineConfig(({ command, mode }) => {
   // Load env file based on `mode` in the current working directory.
   const env = loadEnv(mode, process.cwd(), '')
+  const host = env.HOST || '0.0.0.0'
+  const proxyHost = host === '0.0.0.0' ? 'localhost' : host
   
   return {
     plugins: [react()],
     server: {
-      host: env.HOST || '0.0.0.0',
+      host,
       port: parseInt(env.VITE_PORT) || 5173,
       proxy: {
-        '/api': `http://localhost:${env.PORT || 3001}`,
+        '/api': `http://${proxyHost}:${env.PORT || 3001}`,
         '/ws': {
-          target: `ws://localhost:${env.PORT || 3001}`,
+          target: `ws://${proxyHost}:${env.PORT || 3001}`,
           ws: true
         },
         '/shell': {
-          target: `ws://localhost:${env.PORT || 3001}`,
+          target: `ws://${proxyHost}:${env.PORT || 3001}`,
           ws: true
         }
       }
     },
🧹 Nitpick comments (1)
server/index.js (1)

1801-1829: Consider a user-friendly URL when HOST is 0.0.0.0.
Line 1829 will print http://0.0.0.0:PORT, which isn’t a connectable URL for most users. A display host (e.g., localhost when bound to all interfaces) avoids confusion.

♻️ Suggested tweak
-const HOST = process.env.HOST || '0.0.0.0';
+const HOST = process.env.HOST || '0.0.0.0';
+const DISPLAY_HOST = HOST === '0.0.0.0' ? 'localhost' : HOST;
...
-            console.log(`${c.info('[INFO]')} Server URL:  ${c.bright('http://' + HOST + ':' + PORT)}`);
+            console.log(`${c.info('[INFO]')} Server URL:  ${c.bright('http://' + DISPLAY_HOST + ':' + PORT)}`);

When HOST is set to a specific interface (e.g., 192.168.1.100), the Vite
proxy needs to connect to that same host, not localhost. This fix:

- Adds proxyHost logic that uses localhost when HOST=0.0.0.0, otherwise
  uses the specific HOST value for proxy targets
- Adds DISPLAY_HOST to show a user-friendly URL in console output
  (0.0.0.0 isn't a connectable address)

Addresses CodeRabbit review feedback.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@blackmammoth blackmammoth self-assigned this Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Allow configurable HOST/IP binding

2 participants