R
Railway3mo ago
Sid

What port does railway expose for node apps?

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);
});
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);
});
Solution:
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...
Jump to solution
9 Replies
Percy
Percy3mo ago
Project ID: bd3d866d-5a9a-4ddc-8e35-c05a24db4b74
Sid
SidOP3mo ago
bd3d866d-5a9a-4ddc-8e35-c05a24db4b74
Sid
SidOP3mo ago
No description
Sid
SidOP3mo ago
Here are the deploy logs which show it's running Thinking it might be a port mismatch or something
Solution
MantisInABox
MantisInABox3mo 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
SidOP3mo ago
Ah I see
MantisInABox
MantisInABox3mo ago
If you want to continue using 3000 as your port, you must set a PORT environment variable as 3000 in your service
Sid
SidOP3mo ago
Makes sense, thanks
MantisInABox
MantisInABox3mo ago
No problem
Want results from more Discord servers?
Add your server