0

In node.js, I can use

app.route('/').get(getObjectList).post(postAddObject).put(putDeleteObject);

but I don't know how to trigger ...put(putDeleteObject) in html.

Is it true that we are limited to post and get in html? When can I then use put? Is it only for REST requests through, for instance, jQuery?

How do you normally delete objects? I think it is more safe to go through a post request rather than a get request, but it would be great to chain it like I did in the code example, however, it is probably not possible if the html cannot distinguish between post and put. I have seen some examples where they use

<form method="post">
  <input type='hidden' name='_method' value='put'>
  <button type='submit'>Delete</button>
</form>

but it doesn't work for me.

Edit

I meant to use DELETE and not PUT, but the problem stay the same. I don't know how to change my html to support the DELETE request.

mortensen
  • 1,167
  • 2
  • 13
  • 23
  • To support PUT and DELETE verbs, you can use [method-override](https://github.com/expressjs/method-override) middleware. Side-remark: nothing is stopping you from using PUT to delete, but it's not following the standards i.e. GET for retrieving, POST for adding, PUT for updating, and DELETE for deleting. – Mikey Oct 09 '16 at 08:47
  • Sorry. My mistake. I meant to use the `delete` request for deleting. But how do I change my html to work with this? – mortensen Oct 09 '16 at 08:57
  • read this: [Should PUT and DELETE be used in forms?](http://stackoverflow.com/a/5163030/6567275) conclusion: use AJAX – Thomas Oct 09 '16 at 09:19
  • If you are using method-override middleware, just follow their examples. – Mikey Oct 09 '16 at 13:58
  • @mortensen Did [my post below](http://stackoverflow.com/questions/39941368/using-put-to-delete-in-node-js/39944440#39944440) answer your question? If it did then you may consider [accepting the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) because right now other people searching for this problem see that your question has no good answer and may not read it. If it didn't answer your question then please comment on what is missing. I'm going through my old answers and I want to make sure they are good or need improvements. Thanks. – rsp Nov 10 '16 at 09:14

1 Answers1

1

You didn't include it in the question but it looks like you're using Express. In that case you can use:

var methodOverride = require('method-override')
app.use(methodOverride('_method'))

And then you'll be able to use HTML forms like this one:

<form method="POST" action="/your/resource?_method=DELETE">
  <button type="submit">Delete</button>
</form>

And this in your route handlers:

app.route('/')
  .get(getObjectList)
  .post(postAddObject)
  .put(putObject)
  .delete(deleteObject);

If you want to use:

<form method="POST">
  <input type="hidden" name="_method" value="delete">
  <button type="submit">Delete</button>
</form>

then it's a little bit more complicated but still possible - for more info see:

rsp
  • 107,747
  • 29
  • 201
  • 177