Step 6 - Let’s move the backend into our own app
Course: NextJS (Server side) - chapter 6
We want to introduce a route that returns hardcoded values for a user’s details (email, name, id)
- Introduce a new folder called
api - Add a folder inside called
user - Add a file inside called
route.ts - Initialize a
GETroute inside it
export async function GET() { return Response.json({ username: "harkirat", email: "harkirat@gmail.com" })}- Try replacing the api call in
page.tsxto hit this URL
async function getUserDetails() { try { const response = await axios.get("http://localhost:3000/api/user") return response.data; } catch(e) { console.log(e); }}💡 This isn’t the best way to fetch data from the backend. We’ll make this better as time goes by