SolidJSS
SolidJSβ€’10mo agoβ€’
13 replies
𝐗-π₯𝐞𝐦

Solid start call

Just getting into solid start. Going through this documentation to try and get api routes set up so I can fetch data: https://docs.solidjs.com/solid-start/building-your-application/api-routes

I have two files that look like this
// src/routes/index.tsx
import { createAsync, query } from "@solidjs/router";
import { For } from "solid-js";

const getPosts = query(async () => {
  const posts = await fetch("http://localhost:3001/api/hello");
  return await posts.json();
}, "posts");

export default function Home() {
  const posts = createAsync(() => getPosts());

  return (
    <section>
      <ul>
        <For each={posts()}>{(post) => <li>{post.title}</li>}</For>
      </ul>
    </section>
  );
}

// src/routes/api/hello.ts
import { APIEvent, json } from "solid-start";

export async function GET() {
  console.log("testing")
  return json({ message: "Hello from the backend!" });
}

export async function POST({ request }: APIEvent) {
  const body = await request.json();
  return new Response(JSON.stringify({ echo: body }), {
    headers: { "Content-Type": "application/json" }
  });
}


I can't seem to get it to call the GET (or POST) route. Not sure where I'm going wrong. Thanks for any help!
Documentation for SolidJS, the signals-powered UI framework
Was this page helpful?