创建一个在express.js中使用http DELETE动词的href链接 [英] Create a href link that uses http DELETE verb in express.js

查看:113
本文介绍了创建一个在express.js中使用http DELETE动词的href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用express.js,使用handlebars.js作为我的模板
我想创建一个删除链接,它应该是宁静的,并使用http动词DELETE
而不是GET。



我终于想出了一个方法来实现它,通过创建如下的链接,
添加data-method的delete,然后在客户端使用jquery拦截$

我的链接是:

 < a href =/ destroy / {{_ id}}data-method =delete>删除< / a> 

< script src =http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>
< script>
$(a [data-method ='delete'])。click(function(){
$ .ajax({
url:this.getAttribute('href'),
类型:'删除'
})
返回false
})
< / script>

这是一个在express.js / handlebars.js中实现这个建议的方法吗?一个更好的方法来实现这一点?,我一直无法找到类似于Rails在其帮助程序中的方式:

  link_to(Destroy,http://www.example.com,method :: delete)


解决方案

感谢@tikider我根据您的建议做了更多的研究,并且遇到了一个名为method-override的中间件( https://github.com/expressjs/method-override )由于我想处理DELETE,因此您的建议非常完整,因为表单无法发送DELETE



这就是我解决它的方法,首先我安装了npm的方法覆盖:

  sudo npm install方法覆盖 - 保存

附加到我的应用程序中间件处理程序:

  // ov erride POST有?_method = DELETE 
app.use(methodOverride('_ method'));

然后我的视图将我的删除按钮设置为

 < form method =POSTaction =/ destroy / {{_ id}}?_ method = DELETE> 
< button type =submit>删除< / button>
< / form>

现在,每次点击删除时,它都会通过方法覆盖更改为DELETE,并使用中间件所以在我的应用程序:

  app.delete('/ destroy /:id',function(req,res){
// ...
}

当我使用handlebars模板时,将其添加为辅助方法。


I just started playing with express.js using handlebars.js as my template I wanted to create a delete link, which should be restful and use http verb DELETE instead of GET.

I finally figured out a way to achieve it by creating a link like so below, adding data-method of delete, and then using jquery on client side to intercept my link to use DELETE verb.

My link was:

<a href="/destroy/{{_id}}" data-method="delete">delete</a>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("a[data-method='delete']").click(function(){
    $.ajax({
        url: this.getAttribute('href'),
        type: 'DELETE'
    })
    return false
})
</script>

Is this a recommendable way of achieving this in express.js/handlebars.js?, does anyone have a better way of achieving this?, I haven't been able to find something similar to how rails does it within its helper like:

link_to("Destroy", "http://www.example.com", method: :delete)

解决方案

Thanks @tikider I did more research based on your suggestion and came across a middleware called method-override (https://github.com/expressjs/method-override) Since I wanted to handle DELETE your suggestion was quite complete since forms can't send DELETE

This is how I solved it, first I npm installed method-override:

sudo npm install method-override --save

Attached it to my app middleware handler:

// override with POST having ?_method=DELETE
app.use(methodOverride('_method'));

Then my view I set my delete buttons as

<form method="POST" action="/destroy/{{_id}}?_method=DELETE">
  <button type="submit">Delete</button>
</form>

Now every time I click delete it is changed by method-override to DELETE and caught using a middleware like so within my app:

app.delete('/destroy/:id', function(req, res) {
   //...
}

As I am using handlebars template I can then add it as a helper method.

这篇关于创建一个在express.js中使用http DELETE动词的href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆