Reputation: 33
I'm using express for my project. I didn't follow the rules of creating REASTFUL api since begging of project.Now I'm trying to update my fields with PUT method and express is sending 404 message(cannot GET ...).And this is happening to my DELETE method too. Here goes my view page code for updating :
<form action="/edited/<%= resul[0].name %>" method="PUT">
<label class="label">Name</label>
<input class="input" type="text" name="name" " value="<%= resul[0].name %>">
<label class="label">description</label>
<input class="input" type="text" name="desc" value="<%= resul[0].desc %>">
<button type="submit">DONE</button>
</form>
And here is express part (by the way I'm using Rethinkdb):
app.put("/edited/:name", function(req , res){
r.table('words').filter({name: req.params.name}).update([
{ name: req.body.name, desc:'some update on description'}
]).run().then(function(result,err){
if(err) throw err;
res.send('edited correcly');
});
});
When I try to update something , uri changes like :
http://localhost:3000/edited/test?name=changedtest&desc=something
And error is showsn:Cannot GET /edited?name=changedtest&desc=something
Same thing happens for DELETE.
And surprisingly when I implemented deleting with GET method that worked.
What's going on? how to solve this ?
Do we have to use exact specified http method for specific tasks?(like always DELETE for deleting and...)
Thank you.
Upvotes: 0
Views: 2774
Reputation: 50
As other's mentioned, HTML forms don't support PUT methods. If you want to stick with RESTful routing, there is a great middleware called Method Override, which does exactly that.
All you do is set up the middleware by telling it what string to look for in the query:
app.use(methodOverride("_method"));
Then, on your form, do something like this:
<form action="/edited/<%=item._id%>?_method=PUT" method="POST" >
This will allow you to leave your PUT route as is.
Upvotes: 1