Welcome to the Linux Foundation Forum!

Lab-7.1: Failed to fetch url

this is my solution for lab-7.1
and i start the application using this script
"dev": "BOAT_SERVICE_PORT=3333 BRAND_SERVICE_PORT=3334 fastify start -w -l info -P app.js"

  1. 'use strict'
  2.  
  3. const {
  4. BOAT_SERVICE_PORT,
  5. BRAND_SERVICE_PORT
  6. } = process.env
  7. const schema = {
  8. params: {
  9. type: 'object',
  10. additionalProperties: false,
  11. properties: {
  12. id: { type: 'integer' }
  13. }
  14. }
  15. }
  16. const boatUrl = `http://localhost:${BOAT_SERVICE_PORT}`;
  17. const brandUrl = `http://localhost:${BRAND_SERVICE_PORT}`;
  18. module.exports = async function (fastify, opts) {
  19. fastify.get('/:id', {schema: schema}, async function (request, reply) {
  20. const {id} = request.params;
  21. const boatResponse = await fetch(boatUrl + '/' + id);
  22. if (boatResponse.status === 404) {
  23. console.log('not found')
  24. throw fastify.httpErrors.notFound()
  25. }
  26. const boatResult = await boatResponse.json();
  27. const brandResponse = await fetch(brandUrl + '/' + boatResult.brand);
  28. if (brandResponse.status === 404) {
  29. throw fastify.httpErrors.notFound();
  30. }
  31. const brandResult = await brandResponse.json();
  32.  
  33. return {
  34. id: id,
  35. color: boatResult.color,
  36. brand: brandResult.name
  37. }
  38. })
  39. }
  40.  

but i got this error

  1. ERROR (1934): fetch failed
  2. reqId: "req-1"
  3. req: {
  4. "method": "GET",
  5. "url": "/1",
  6. "hostname": "localhost:3000",
  7. "remoteAddress": "::1",
  8. "remotePort": 52469
  9. }
  10. res: {
  11. "statusCode": 500
  12. }
  13. err: {
  14. "type": "TypeError",
  15. "message": "fetch failed: connect ECONNREFUSED ::1:3333",
  16. "stack":
  17. TypeError: fetch failed
  18. at Object.fetch (node:internal/deps/undici/undici:11576:11)
  19. at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  20. at async Object.<anonymous> (/Users/ibrahim/node-training/lab-7.1/routes/root.js:21:26)
  21. caused by: Error: connect ECONNREFUSED ::1:3333
  22. at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)

any thoughts ?

Comments

  • Posts: 7
    edited January 2024

    Your Error status code response is 500, which for HTTP status codes is an Internal Server Error. Are you certain that local server is up & running and that it is set up to receive this routed GET request? Also, you got this fetch from whichever localhost you have set up on Port 3000 — is that the BOAT_SERVICE_PORT localshost server or the BRAND_SERVICE_PORT localhost server? I'd look at whichever one of those is on localhost:3000 and start there.

  • Posts: 160

    if you have connection refused it means you're not running the necessary services on those ports

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training