Fixing "Connection Refused" Errors with Serverless Offline
Umair Syed

Umair Syed @umairian

About: Senior Full Stack AI Engineer

Location:
Pakistan
Joined:
Sep 9, 2024

Fixing "Connection Refused" Errors with Serverless Offline

Publish Date: Jan 28
1 3

If you've ever encountered the frustrating ECONNREFUSED error while working with Serverless Offline, you're not alone. I was getting this error when I tried to run e2e tests with cypress. The frontend was working fine and was able to hit the serverless offline backend but in cypress tests, it was giving refused connection errors. In this blog, I'll walk you through how I solved this problem and got my serverless backend up and running locally.

The Problem

While running my e2e cypress tests locally, I kept running into the following error:

Error: connect ECONNREFUSED 127.0.0.1:8080
Enter fullscreen mode Exit fullscreen mode

This error indicated that my application (or Cypress tests) couldn't connect to the backend running on localhost:8080. Surprisingly, the backend worked fine when tested manually with curl or Postman or browser, but it failed when accessed via Cypress or other tools.

The Solution

After some debugging, I discovered two key changes that resolved the issue:

1. Change the Port

By default, sls offline runs on port 8080. However, this port might already be in use by another process on your machine. To fix this, I updated the serverless.yml file to use a different port (e.g., 8000):

custom:
  serverless-offline:
    httpPort: 8000
Enter fullscreen mode Exit fullscreen mode

2. Set the Host Explicitly

Sometimes, sls offline binds to localhost by default, which can cause issues in certain environments. To ensure the server is accessible from all network interfaces, I explicitly set the host to 0.0.0.0:

custom:
  serverless-offline:
    httpPort: 8000
    host: 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

After making these changes, I restarted sls offline, and there you go! The backend was now accessible at http://localhost:8000.

Why This Works

  • Changing the Port: If port 8080 is already in use, switching to a different port (e.g., 8000) ensures there’s no conflict.
  • Setting the Host to 0.0.0.0: Binding to 0.0.0.0 makes the server accessible from any network interface, not just localhost. This is especially useful when working with tools like Cypress or Docker.

Steps to Reproduce the Fix

  1. Update your serverless.yml file:
   custom:
     serverless-offline:
       httpPort: 8000
       host: 0.0.0.0
Enter fullscreen mode Exit fullscreen mode
  1. Restart sls offline:
   sls offline
Enter fullscreen mode Exit fullscreen mode
  1. Test the backend using curl or your application:
   curl -X POST http://localhost:8000/prod/signIn \
     -H "Content-Type: application/json" \
     -d '{"email": "test@example.com", "password": "your-password"}'
Enter fullscreen mode Exit fullscreen mode

You can also resolve the issue via terminal command by running sls offline with specific options directly. This can be done in one line like so:

sls offline start --httpPort 8000 --host 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

This command will start your serverless offline environment on port 8000 and bind it to 0.0.0.0, making the backend accessible from all network interfaces without the need to modify the serverless.yml file manually. It's a quick and efficient alternative for testing.

Bonus Tip: Debugging with Cypress

If you're using Cypress for end-to-end testing and still encountering issues, try hitting the backend directly:

cy.request({
        method: 'POST',
        url: 'http://localhost:8000/api/signIn', // Replace with your backend URL
        body: {
          email: Cypress.env("TEST_USER_EMAIL"),
          password: Cypress.env("TEST_USER_PASSWORD"),
        },
      }).then((response) => {
        expect(response.status).to.equal(200);
        expect(response.body.token).to.exist;
      });
Enter fullscreen mode Exit fullscreen mode

Comments 3 total

  • Ijaz Ahmad Afridi
    Ijaz Ahmad AfridiJan 28, 2025

    And sir what about MongoDB to use it offline . I used it offline for many project using mongodbcompass and mongosh but sir now it throws en error while connecting to it through mongoose or even through GUI in MongoDB compass
    Image description

    • Umair Syed
      Umair SyedJan 28, 2025

      That is because mongodb server is not running. Either you didn't install it or it is installed but not started.

      • Ijaz Ahmad Afridi
        Ijaz Ahmad AfridiJan 28, 2025

        Sir then how to start it as I am already in mongodbcompass

Add comment