Skip to content

POST handler executed instead of GET handler #6507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skanderturki opened this issue May 9, 2025 · 4 comments
Closed

POST handler executed instead of GET handler #6507

skanderturki opened this issue May 9, 2025 · 4 comments
Labels

Comments

@skanderturki
Copy link

I have sent the full code below. To reproduce the strange behavior:
Run both the server and the client, send a request throw the form , check that you are sending a GET request using developer tools then, on the server check that the app.post handler is being executed. Note that there ia a problem in the form, it is not executing the js script because of a missing property in the button (type="button"), so the type of the button is by default "submit" and the request that is being sent is a GET request sending the data on the URL query part. But why is the server executing the app.post handler?

Environment information

Version:

"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"mysql2": "^3.14.0",
"sequelize": "^6.37.7"
}

Platform:
Microsoft Windows NT 10.0.26100.0
x64

Node.js version:
v22.14.0

Any other relevant information:
Full code is here:
Description: a server app and a client app, just add a MySQL server connection details or simply remove connection to DB as it is not relevant here:
https://drive.google.com/file/d/1P-50C95h5RAMYarO-scfUHaLLPq1mmZF/view?usp=sharing

@krzysdz
Copy link
Contributor

krzysdz commented May 9, 2025

This sounds impossible and I can't reproduce it with your example. Could you add some logging to your server's app.js to check request method and URL?

// Add this before your route definitions
app.use((req, res, next) => {
  console.log(req.method, req.url, req.headers['user-agent'])
  next()
})

BTW please don't send archives (or links to archives) on GitHub - include short snippets of code or use a separate repo/gist.

@skanderturki
Copy link
Author

Ok, you were right, something I don't get is happening at the client side, so nothing to do with express: here is the listing you asked for, it shows that the server is actually getting a POST request:
POST /api/v1/countries/name/ert/phonecode/123 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

And here is a video that shows the strange behavior (caused by the client ..or my misunderstanding of what the browser is doing):
https://youtu.be/bN2DTRrZm28

@krzysdz
Copy link
Contributor

krzysdz commented May 10, 2025

The User Agent is one of a browser. Your video shows that the browser performs a fetch request to URL ending with /45 that originates from countries.js:11, just before navigating to /?name=rt&phonecode=45. This later disappears after the document loads, because Preserve log is unchecked.

Browser DevTools with network tab showing a fetch request originating from JS to "45" and a document request to the page URL with appended query parameters

I'd suggest using submit event to process form submission and prevent default behaviour.

@skanderturki
Copy link
Author

Thank a lot that was very helpful, I am closing this thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants