Search code examples
node.jsserver-side-renderingastrojs

Astro server endpoints with nodejs adapter has empty header


I am following this tutorial: [Astro with Firebase][1]https://docs.astro.build/en/guides/backend/google-firebase/ and everything works as expected after I run npm run dev. However when I run npm run build and start the server with node ./dist/server/entry.mjs I am not able to access headers from the request sent to server. Here is the server endpoint code:


export const get: APIRoute = async ({ request, cookies, redirect }) => {
  /* Get token from header*/
  const idToken = request.headers.get("Authorization")?.split("Bearer ")[1];
  if (!idToken) //idToken null
{
    return new Response(
      "No token found",
      { status: 401 }
    );
  }
 ...
  return redirect("/dashboard");
};

I am running newest version of Astro with SSR nodejs (hybrid rendering.

So far I tried:

  1. Use axios instead of request for HTTP get request
  2. Different version of node (18 /20)
  3. Add token as a param after my base url (localhost:3000/api/auth?token) - it seems that only plain URL is available on the server side.

Thanks for any ideas.


Solution

  • I find the solution after reading the documentation of Astro more thoroughly. The hybrid rendering set in my astro.config.mjs by default renders everything as static content even the server endpoints, which was not clear to me from the documentation. So basically configuring output: 'hybrid' we have to tell Astro which routes should be SSR, even the server endpoints. More information here: Astro - Opting out of pre-rendering

    So in my case the solution was to add this line to my server endpoints and everything works great:

    export const prerender = false; //ADDED LINE
    export const get: APIRoute = async ({ request, cookies, redirect }) => {
    ...