W
Wasp-lang•5mo ago
Ghar

First time wasp user, Unable to add super simple custom API

First time using wasp :) so it is probably user error. I am trying to add a basic API endpoint as follows. In app/main.wasp I have
api helloWorld {
fn: import { helloWorld } from "@src/server/apis.ts",
httpRoute: (GET, "/hello-world")
}
api helloWorld {
fn: import { helloWorld } from "@src/server/apis.ts",
httpRoute: (GET, "/hello-world")
}
in app/src/server/apis.ts I have
import { Request, Response } from 'express';

export const helloWorld = async (_req: Request, res: Response) => {
res.setHeader('Content-Type', 'application/json');
res.status(200).json({ message: "Hello World!" });
};
import { Request, Response } from 'express';

export const helloWorld = async (_req: Request, res: Response) => {
res.setHeader('Content-Type', 'application/json');
res.status(200).json({ message: "Hello World!" });
};
I am expecting a JSON response but instead get HTML curl http://localhost:3000/hello-world returns html not the expected json browsing http://localhost:3000/hello-world returns an html page with layout. Additonal info: Request URL: http://localhost:3000/hello-world Request Method: GET Status Code: 304 Not Modified Remote Address: 127.0.0.1:3000 Referrer Policy: strict-origin-when-cross-origin
3 Replies
kapa.ai
kapa.ai•5mo ago
Hi @Ghar. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information you've provided, it seems like you've correctly set up your custom API endpoint in Wasp. However, it's possible that there might be some other issue causing the HTML response instead of the expected JSON. Here's a quick checklist to troubleshoot the issue: 1. Ensure that your Wasp project is running without any errors. You can do this by checking the terminal where you ran the wasp start command. 2. Make sure that the apis.ts file is correctly placed in the src/server directory and that the helloWorld function is correctly exported. 3. Verify that the api declaration in your main.wasp file is correctly formatted and doesn't have any syntax errors. The api declaration should look like this:
api helloWorld {
fn: import { helloWorld } from "@src/server/apis.ts",
httpRoute: (GET, "/hello-world")
}
api helloWorld {
fn: import { helloWorld } from "@src/server/apis.ts",
httpRoute: (GET, "/hello-world")
}
4. Check if there are any other routes or APIs in your Wasp project that might be conflicting with the /hello-world route. If you've checked all of these and you're still facing the issue, it might be a good idea to raise this issue on the Wasp Github (https://github.com/wasp-lang/wasp/issues) or ask for help in the Wasp Discord (https://discord.com/channels/686873244791210014) community. The Wasp community is very active and helpful, and you're likely to get a solution to your problem quickly. For more information on creating custom API endpoints in Wasp, you can refer to the Wasp documentation (https://wasp-lang.dev/docs/advanced/apis). Sources: - Wasp Documentation - Custom HTTP API Endpoints (https://wasp-lang.dev/docs/advanced/apis) - Wasp Documentation - API Reference (https://wasp-lang.dev/docs/advanced/apis#api-reference) - Wasp Discord (https://discord.com/channels/686873244791210014) - Wasp Github (https://github.com/wasp-lang/wasp/issues) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Ghar, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•5mo ago
Hey, your sever is running on port 3001 and not port 3000 😊 Try it with localhost:3001 and let me know how it goes.
Ghar
GharOP•5mo ago
thanks! that worked :) WTH?
Want results from more Discord servers?
Add your server