405 Error for POST request
I'm working through a Coding Train tutorial on data and APIs, but I got to a point where I keep getting a 405 error when trying to make a POST request.
index.js:
script inside index.html:
In the console I get:
geolocation available
[my lat] [my lng]
POST http... net::ERR_ABORTED 405 (Method Not Allowed)
If I go to Network in the dev tools in Chrome and open the item with the error I can see that the Response Headers > Allow: GET, HEAD, OPTIONS.
I'm not sure why POST is not allowed.
Any help would be greatly appreciated. Thanks!65 Replies
POST is generally used to make changes on the remote server, not to request data
99% of the free APIs out there are read-only, so they don't tend to use POST as a method at all
I'm unfamiliar with the tutorial, but are you sure it's using POST in the example code?
Also, as all HTTP errors, a 405 error is a server side error. Unless you control the API, you will have to check the documentation for the API to see what is and isn't allowed at that end point and adjust your own code accordingly
They're using their own nodejs server
oh! I missed that part, oops
that's true, I'm both server and client for this project
I don't know express but i don't see anything wrong right away
same, nothing super obvious at least
have you restarted the express server?
I'm using nodemon
it restarts regularly
should I manually restart it?
try sending a get request and see what happens
Why are you listening the server before defining the middleware
yeah
also, the post is after the listen too
Not sure if that makes a difference with express's implementation but usually you listen at the very end
might not help, but it's pretty much free to test and restarting shit solves 90% of all issues
(ignore the low fps)
(and the misspells)
Also a note that with latest node version, nodemon isn't needed
Just use the --watch option
I manually restarted it, same issue
move the listen to the end
moved listen to the end, same 405 error
send a get request
--watch was introduced in Node v18, back in 2022. Stable in Node 20, back in 2023 😉Can you post the console error output here if you get 405 again
index.html:32
POST http://127.0.0.1:5500/api net::ERR_ABORTED 405 (Method Not Allowed)
(anonymous) @ index.html:32
There's your problem
I know but I can't be bothered to look up the date so saying latest version is a good bet. Tho i actually meant lts. Keep confusing latest and lts
You're posting to port 5500 but the server is listening on port 3000
Yep. That's why I did the hard work 😉
I welcome the service
I'd suggest typing out the entire URL, not doing relative, when using localhost
probably a dumb question, how do I change what port I'm posting at?
new error "localhost" is not supported
try
http://localhost:3000/apii see the problem
you have to specify the full api url
Might also have to explicitly put the ip address instead of localhost if it says cors has blocked request
Frickin' CORS 😡
I'm using live server in VS code at port:5500. Is that a problem?
No that's okay
you're sending the request to
/api, which is a relative path to the root of the live version
but you're hosting it in a different "host" (port)
you have to do this: specify the FULL url with port
you can't just do /api, because it will send the request to the server hosting the website insteadindex.html:1 Access to fetch at 'http://localhost:3000/api' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
do I need to add something to the headers in options?
you have to send headers for cors from the server, and you have to request cors from the fetch
Or set the IP instead of
localhostthat'll be the same
because the port is different, it's another host
also, 127.0.0.1 and localhost are not the same origin for this
Yeah he will need the cors middleware
Actually not even middleware if we're just doing a single endpoint. Just set it on response
I'm actually wondering
Is the tutorial actually means you to server html file from the public directory?
You have express static in the code
I was wondering about that, too. They're using the static middleware so the server should be serving it and not VS Code
This here seems to be the problem. Turn off VS Code server and let your Express server do it
Then it's on the same origin and it'll work fine
We'll need the file structure first
To confirm they actually have html in public folder
I would hope the tutorial would show how to setup the file structure properly lol
Me too but i dunno what the tutorial intended lol
static index.html is in the public folder
how do I 'let Express server do it'
you run node from the console
You already are:
Is where your server is running
If you type in browser url bar it should serve the html file
you forgot the
// partAhh dammit
still wrong
it's
://, not //:it worked!
thanks so much!
you guys are the best!
We're ok, I guess 🤷
I still have a lot to learn
You're in the right place to keep learning
any recommendations for tutorials I should follow for learning backend
Keeps happening
it's fine
i have almost 19 years of programming in my belt, but i still have a lot to learn
Not really sure. I learned most of Nodejs by reading the docs. A lot of tutorials will start with
npm i <a long list of packages> so they don't actually teach you Nodejs, they teach you how to use other people's packages.
For example, you really don't need Express in 2025. Nodejs has 90% of it already covered, except for serving static files, and even then that's really easy to do 🤷MDN Web Docs
Server-side website programming - Learn web development | MDN
The Dynamic Websites – Server-side programming topic is a series of modules that show how to create dynamic websites; websites that deliver customized information in response to HTTP requests. The modules provide a general introduction to server-side programming, along with specific beginner-level tutorials on how to use the Django (Python) an...
This might be okay
Note i haven't taken it but at a cursory glance it seems fine
Just ignore the non node js parts like django
thanks again!