What port does railway expose for node apps?
sid-turner-ellis
HOBBYOP

a year ago

I have a very simple express app, I have it in a monorepo just to test some stuff, it builds fine, it starts fine, but I get the "application failed to respond" error when going to it on the provided domain

I'm logging all traffic, and I can see that I'm not getting any when navigating to the domain

I've tested the build locally, and it builds and starts just fine

Any ideas?

import express from "express";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express();
const PORT = 3000;

app.use(express.static(path.join(__dirname, "dist")));

app.get("*", (req, res) => {
  console.log("Request made: ", req.path);
  res.sendFile(path.join(__dirname, "dist", "index.html"));
});

app
  .listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
  })
  .on("error", (err) => {
    console.log(`Error: ${err}`);

    process.exit(1);
  });

9 Replies

sid-turner-ellis
HOBBYOP

a year ago

bd3d866d-5a9a-4ddc-8e35-c05a24db4b74


sid-turner-ellis
HOBBYOP

a year ago

1283432966932004900


sid-turner-ellis
HOBBYOP

a year ago

Here are the deploy logs which show it's running


sid-turner-ellis
HOBBYOP

a year ago

Thinking it might be a port mismatch or something


a year ago

Each application will be assigned a random port number. Unless you specify. Your code above sets the port variable. You would want to do something like

const PORT = process.env.PORT || 3000;

Then when you deploy, it will use the railway provided port, which will automatically display in the selection when you add a domain


sid-turner-ellis
HOBBYOP

a year ago

Ah I see


a year ago

If you want to continue using 3000 as your port, you must set a PORT environment variable as 3000 in your service


sid-turner-ellis
HOBBYOP

a year ago

Makes sense, thanks


a year ago

No problem


Loading...