OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to fetch an endpoint in another endpoint?

  • Thread starter Thread starter John Winston
  • Start date Start date
J

John Winston

Guest
I have an endpoint in /api/metadata, and I want to fetch it in another endpoint like this:

Code:
export const get: RequestHandler = async (event) => {
    try {
        const res = await fetch(event.url + '/api/metadata')

        const body = await res.json()

        return {
            body
        };
    } catch (error) {
        console.error(error);
        return {
            status: 500
        };
    }
};

But I got the following error:

Code:
FetchError: request to http://localhost:3000/sitemap.xml/api/metadata failed, reason: connect ECONNREFUSED ::1:3000

When I change the fetch location to http://0.0.0.0:3000/api/metadata the fetch would just work. Is it possible for me to handle this fetch request correctly without using conditional with process.env.NODE_ENV(the dev in $app/env)?

<p>I have an endpoint in <code>/api/metadata</code>, and I want to fetch it in another endpoint like this:</p>
<pre class="lang-js prettyprint-override"><code>export const get: RequestHandler = async (event) => {
try {
const res = await fetch(event.url + '/api/metadata')

const body = await res.json()

return {
body
};
} catch (error) {
console.error(error);
return {
status: 500
};
}
};
</code></pre>
<p>But I got the following error:</p>
<pre><code>FetchError: request to http://localhost:3000/sitemap.xml/api/metadata failed, reason: connect ECONNREFUSED ::1:3000
</code></pre>
<p>When I change the fetch location to <code>http://0.0.0.0:3000/api/metadata</code> the fetch would just work. Is it possible for me to handle this fetch request correctly without using conditional with <code>process.env.NODE_ENV</code>(the <code>dev</code> in <code>$app/env</code>)?</p>
 

Latest posts

Top