Nodejs app refuse connection after deployment

Trying to deploy a Nodejs application but facing the below error:

[error] instance refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

fly.toml

app = "monbul-one"
primary_region = "sjc"

[build]

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

Dockerfile

# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=18.14.2
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci

# Copy application code
COPY --link . .


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 8080
CMD [ "node", "server.js" ]

In server.js file

const port = process.env.port || 8080;

This is a very simple application and works perfectly locally. What am I missing here?

Most common error is that you are listing on localhost instead of 0.0.0.0 which means that your application will only accept connections made from the same machine, not the internet.

It is also possible that your application threw an exception before it got to the point where it called listen.

Iā€™d need to see more of your logs and/or more of your server.js to help further.

Alternately I can offer you an example node application that you can see working:

mkdir demo
cd demo
npx --yes @flydotio/node-demo@latest

That application can be configured with a lot of options (example --express will convert it to use express as the web server), so perhaps you can get a configuration that closely matches your actual usage and then copy from that. See the following blog post for more details:

I did follow the above mentioned blog. Did deploy both the apps. The only difference is flydotio project installs npm packages during flyctl launch while my app fails to install npm packages.

I guess this is the issue for the failure. What should I do sothat flyctl launch runs npm install in my app also?

What error are you getting?

Not getting any error but npm install step is skipped

āžœ  nodejs-app git:(feature/config-changes) flyctl launch 
Creating app in /Users/len/projects/iynthogai/nodejs-app
Scanning source code
Detected a NodeJS app
? Choose an app name (leave blank to generate one): iynthogai-node-app
? Select Organization: Monbgul (monbgul)
Some regions require a paid plan (bom, fra, maa).
See https://fly.io/plans to set up a plan.

? Choose a region for deployment: Singapore, Singapore (sin)
App will use 'sin' region as primary

Created app 'iynthogai-node-app' in organization 'monbgul'
Admin URL: https://fly.io/apps/iynthogai-node-app
Hostname: iynthogai-node-app.fly.dev
     create  Dockerfile
Wrote config file fly.toml
Validating /Users/len/projects/iynthogai/nodejs-app/fly.toml
Platform: machines
āœ“ Configuration is valid

If you need custom packages installed, or have problems with your deployment
build, you may need to edit the Dockerfile for app-specific changes. If you
need help, please post on https://community.fly.io.

Now: run 'fly deploy' to deploy your Node.js app.

How to install custom packages?

npm install will be skipped if @flydotio/dockerfile is in your package.json already. Since I see create Dockerfile in your output @flydotio/dockerfile appears to be installed and working correctly.

What does the call to listen look like in your server.js?

My server.js file

var express = require("express");
var app = express();
const hostname = "localhost";
const port = process.env.port || 3000;
const createpdf = require("./src/generatePDF/create-pdf.js");
const cors = require("cors");

app.use(express.json());

app.get("/", cors(), async function (req, res) {
  res.send("Welcome to Iynthogai NodeJS Server");
});

app.post("/getPDF", cors(), async function (req, res) {
  createpdf
    .createPDF(req)
    .then(function (pdfbuffer) {
      res.status(200);
      res.type("pdf");
      res.send(pdfbuffer);
    })
    .catch(function (err) {
      res.status(500);
      res.send(err.message);
    });
});

app.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

app.use(
  cors({
    origin: "*",
    methods: ["GET", "POST"],
  })
);

Also, my application monitoring output says

2023-08-28T14:24:18.536 app[91857d7f699383] sin [info] Server running at http://localhost:3000/

while flydotio app monitoring output says

 2023-08-28T14:46:42.438 app[178113d4f59248] sin [info] Server is listening on port 3000 

Try changing that to:

1 Like

Deployed the app and Working as expected. Thank you very much.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.