Netlify dev doesn't seem to have access to localhost port 3000

Hi Team Netlify Support

My site is https://astro-netlify-sandbox.netlify.app/

Site : astro-netlify-sandbox.netlify.app

My node version is v20.5.0

I am returning to netlify after a long time and I seem to have forgotten everything so it’s a fresh start for me… and I am experimenting with functions…

https://astro-netlify-sandbox.netlify.app/.netlify/functions/hello

As you can see the above code works and I’d like to have that work on my localhost too…

so I ran

netlify dev

and it gives me this message

┃ Local    http://localhost:4321/
┃ Network  use --host to expose

11:34:18 watching for file changes...
⠇ Waiting for framework port 3000. This can be configured using the 'targetPort' property in the netlify.toml

And then when I try to use the url below it doesn’t work

http://localhost:3000/.netlify/functions/hello

I have this in the netlify.toml

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

[dev]
  command = "astro dev"
  targetPort = 3000

The netlify.toml file is being read and working fine as you will see that the url below too works on production.

https://astro-netlify-sandbox.netlify.app/api/hello

So the [[redirects]] part from the netlify.toml is being read and used… but for some reason the [dev] part is not working…

I’ve asked your chatbot and also checked the documentation but couldn’t figure out what to do.

Please advise as it seems to work on the production but it’s not working on development.

Regards,
Alim

Looks like you need 4321 and not 3000 as the port.

oh… so the targetport is not something that’s set as 3000 but it is the port where astro creates the local server?..

Sorry, I feel so stupid, right now…;-)… Thanks for pointing that out. I’ve been struggling with this for a couple of days now and couldn’t figure out what I was doing wrong… it’s been a while, I’ve been away from doing this so I guess it’s time to get my act together…;-)…

Thanks Hrishikesh.

Regards,
Alim

Hi Hrishikesh

I’ve tried what you suggested and the hello.mjs function that’s provided in the documenation on netlify functions page works fine on production as well as localhost

Here’s the code

export default async (req, context) => {
  return new Response("Hello, world!");
};

Here’s the link to the production site

https://astro-netlify-sandbox.netlify.app/api/hello

And i’ve attached a screenshot for the localhost view

So that works fine on production and on localhost:8888

Now there’s the other feature on netlify that’s ‘ROUTE REQUESTS’

export default async (req, context) => {
  const { city, country } = context.params;

  return new Response(`You're visiting ${city} in ${country}!`);
};

export const config = {
  path: "/travel-guide/:city/:country"
};

This works fine on the production site… here’s the link

https://astro-netlify-sandbox.netlify.app/travel/mumbai/india

Apologies for the bad use of the definite article ‘the’ there…;-)…

Anyways it doesn’t seem to work on localhost:8888 (screenshot below)

What am I doing wrong? Please advise.

Here’s the exact code that I am using (the above was copy/pasted from netlify site)

export default async (req, context) => {
  const { city, country } = context.params;
  return new Response(`This is the ${city} and this is the ${country}`);
};

export const config = {
  path: "/travel/:city/:country",
};


Regards,
Alim

Hello Hrishikesh,

Any update on this?

Regards,
Alim

Does this work on the default path: /.netlify/functions...? You might want to remove the config to be able to check that.

I am not sure what exactly is the route you want me to test with… but I am assuming you mean this…

http://localhost:8888/.netlify/functions/travel/mumbai/india

Here are the screenshots for the results of that route…

and

Please let me know if you meant something else.

[ADDING THIS LATER] just in case this is what you were looking for…

Regards,
Alim

Hi Hrishikesh

Any update?

Regards,
Alim

Hi Hrishikesh,

Sorry to bother you… but this is just a gentle reminder for my issue.

Regards,
Alim

I believe the problem is that Astro might be using the /api/* routes itself: Endpoints | Docs (astro.build), so it’s probably better to try to use a different route like /api2/* for your Netlify Functions.

Hi Hrishikesh

So based on your suggestion I change the netlify.toml to this

[[redirects]]
  from = "/api2/*"
  to = "/.netlify/functions/:splat"
  status = 200

[dev]
  command = "astro dev"
  targetPort = 4321

so http://localhost:8888/api2/hello works fine

but http://localhost:8888/travel/mumbai/india still shows the same 404 error.

Regards,
Alim

Hi Hrishikesh

Any update on this?

Regards,
Alim

Try putting this in a repo and send it over.

When you say put it in a repo, you mean share the link to the github public repository… am I right? I am just confirming as I am not very conversant with the web dev terminology and hence wanted to make sure.

If the repo is not public, is there a way to ‘put it in a repo’?

I would like to understand the options available to me when someone makes this request, hence the query.

Anyways here’s the github url
GitHub - alim4fourplus/astro-netlify-sandbox

Hope this is what you needed.

Regards,
Alim

Hi Hrishikesh

Just following up on my earlier message. Hope you’ve got what you had asked for. Looking forward to some update on the solution for the issue.

Regards,
Alim

@alimbolar Just letting you know that you don’t need to bump posts here.

There’s no guaranteed response times and the support staff respond when able.

The repository that you’ve provided should be enough, and if it’s not hrishikesh will request additional details.

Thanks Nathan for the update…:-)… really appreciate it…

Have a lovely day…:-)…

Regards,
Alim

When I checked your repo, I am able to load the Functions fine:

What version of the CLI are you using?

Hi Hrishikesh

when I type netlify --version I get this

netlify-cli/15.9.1 darwin-arm64 node-v18.19.0

Regards,
Alim

I updated the versions and now it’s working fine…

netlify-cli/17.11.1 darwin-arm64 node-v20.10.0

My apologies for not checking this right in the beginning… Thanks for your time, effort and patience.

Regards,
Alim