79518344

Date: 2025-03-18 18:12:59
Score: 0.5
Natty:
Report link

I think that the problem is in the way you structure your post route and in which context are you using it..

You can't just type "/add" in your browser address bar. That's a GET request, but your code only handles POST.

You need to add in your index.ejs file a form like this:

<form action="/add" method="POST">
  <input type="text" name="country" placeholder="Enter a country">
  <button type="submit">Add Country</button>
</form>

Just put this form on your page, fill a country name, and submit. That will trigger the POST request your route is expecting. Browser address bars can only do GET requests.

@Extra: If you want to test your POST route directly without creating a form, Insomnia is perfect for that. Just download Insomnia, create a new POST request to http://localhost:3000/add, set the body to 'Form URL Encoded' format, add a key-value pair with 'country' as the key and your country name as the value, then hit send. It's way easier for testing API endpoints than trying to build forms every time. You can test routes almost in real-time, that will boost your production.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Extra
  • Low reputation (1):
Posted by: Rafael