Nextjs and node server not able to communicate over private networking
bertvereecken
TRIALOP

2 years ago

Hi,

Context:

I have a monorepo with turborepo with two apps inside:

  • web (nextjs)
  • backend (node server)

I would like these two to communicate with each other over the private network.

I've read the docs and previous threads, but I am still not able to get it working.

My node server is listening on :: (to allowd IPv6) on port 3001.

In my web package I am fetching the following endpoint;

const baseUrl = env.NEXT_PUBLIC_BACKEND_BASE_URL;

const bookAxiosClient = axios.create({
  baseURL: `${baseUrl}/books`,
});

In my web service in Railway I've set the following env var:

NEXT_PUBLIC_BACKEND_BASE_URL=http://${{Turbo repo BE.RAILWAY_PRIVATE_DOMAIN}}:3001/api

When i check the deployed frontend I see these errors (see attached screenshots)

Hopefully somebody can help me 🙂

Bert

Solved

31 Replies

bertvereecken
TRIALOP

2 years ago

4838d5a0-e5bd-482c-bbf8-35740edea841


2 years ago

if the user's browser is making the api call (it is) then you need to use the public domain, it would not be a very private network if anyones browser could make a fetch call to a domain on the private network


bertvereecken
TRIALOP

2 years ago

Thanks for the swift reply 🙂


bertvereecken
TRIALOP

2 years ago

Makes sense. Networking noob here 😦 Will try that and couple back to you.


2 years ago

sounds good!


bertvereecken
TRIALOP

2 years ago

Mmm, still no success.

So what i did:

  • Changed the env var to the public name like this: ${{Turbo repo BE.RAILWAY_PUBLIC_DOMAIN}}/api

bertvereecken
TRIALOP

2 years ago

Now i receive a 404 error.


bertvereecken
TRIALOP

2 years ago

1317405974155235359


2 years ago

you're calling the frontend domain


2 years ago

I see fe there


bertvereecken
TRIALOP

2 years ago

Huh, how is that possible: i changed my env var to this ?

${{Turbo repo BE.RAILWAY_PUBLIC_DOMAIN}}/api


bertvereecken
TRIALOP

2 years ago

1317406224039415838


2 years ago

then you have to use that in code


bertvereecken
TRIALOP

2 years ago

1317406375458115665


2 years ago

console logging time


bertvereecken
TRIALOP

2 years ago

https://turbo-repo-fe-production.up.railway.app/turborepo-production.up.railway.app/api/books

Seems like nextjs is interfering ? This is a 404 page of next itself.


2 years ago

oh


2 years ago

you forgot to add https:// to the variable in Railway


bertvereecken
TRIALOP

2 years ago

Ok, will try that. Thought it was already included in that variable.


bertvereecken
TRIALOP

2 years ago

1317407850774925362


2 years ago

nope, domain is a domain, not url


2 years ago

also, it's better practice to not put /api there, do that in code


bertvereecken
TRIALOP

2 years ago

Ok, will also do that 🙂


bertvereecken
TRIALOP

2 years ago

You are a hero ! Thanks @Brody

1317408420596289576


2 years ago

no problem!


bertvereecken
TRIALOP

2 years ago

Must say this is 10/10 support. Even answering on a saturday 🙂 Thanks


2 years ago

haha thank you


bertvereecken
TRIALOP

2 years ago

Just a follow up question: in my backend do i still need to bind it to :: now that I am using a public url ? I suppose this can be gone now ?


2 years ago

you dont need to, but it doesn't hurt


bertvereecken
TRIALOP

2 years ago

Ok thanks 🙂


2 years ago

!s


Status changed to Solved brody • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...