Skip to content

Expose Backend Using Cloudflare Tunnel + PM2 (Free & Persistent) #6

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

We need to expose our local Node.js + Express backend (with LangChain integration) to the public using a stable, long-running, and free solution.
The goal is to allow external users, collaborators, or client applications to access the AI backend without requiring local setup, VPN, or port forwarding.


✅ Chosen Solution

  • Cloudflare Tunnel (cloudflared)
    Used for public HTTPS exposure of the local server, enabling secure and reliable access over the internet.

  • PM2 Process Manager
    Runs both the backend server and the Cloudflare tunnel in the background, ensuring they stay up and restart automatically if needed.

🔧 Setup & Configuration Tasks

To expose the backend using Cloudflare Tunnel and manage processes with PM2, follow these steps:

1. Install and Configure Cloudflared CLI

  • Download and install the cloudflared CLI from Cloudflare's official site.
  • Authenticate and create your tunnel according to their documentation.

2. Update Project Scripts or Documentation

  • Ensure your project scripts support starting both the backend and tunnel easily.
  • Example tunnel command:
  cloudflared tunnel --url http://localhost:<PORT>

3. Use PM2 to Run Both Services

  • Run your Node.js backend (e.g., server.js) with PM2:
pm2 start server.js --name backend
  • Run the Cloudflare tunnel with PM2:
pm2 start "cloudflared tunnel --url http://localhost:<PORT>" --name tunnel
  • Make sure the tunnel URL is displayed or logged on startup for easy reference.

4. Keep Services Running After Reboot

  • Use PM2 startup script to launch services on system boot:
pm2 startup
pm2 save

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions