Node.JS中的HTTP DELETE动词 [英] HTTP DELETE verb in Node.JS

查看:95
本文介绍了Node.JS中的HTTP DELETE动词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以让 GET POST PUT 请求,但 DELETE 请求不起作用。



DELETE http:// localhost:8081 / api / 1.0 / entry undefined ,我使用express来注册路由。但是看起来我甚至不能解决url /动词。



这是我如何调用它:


$ b $ ('click',function(){
$ .ajax({
url:' /($)
类型:'DELETE'
})完成(function(res){
var row = $(this).parentsUntil('tbody');
row.slideUp();
});
});

样本日志

 code> GET / 200 18ms 
GET / author / entry 200 10ms
GET /api/1.0/entry 200 2ms
GET /api/1.0/entry 200 1ms
GET /api/1.0/entry 200 1ms
undefined


解决方案

希望这可以帮助:




  • 启用日志记录作为您的第一个中间件,以确保请求进入:



    app.use(express.logger());

    li>
  • 使用methodOverride()中间件:



    app.use (express.bodyParser());



    app.use(express.methodOverride()); //寻找隐藏字段中的DELETE动词


  • 创建一个.del()路由: / p>

    app.del('/ api / 1.0 / entry',function(req,res,next){...}); code>



Do I need to set up any configuration before I can make DELETE requests to a node.js application?

I can make GET, POST or PUT requests, but DELETE requests won't work.

DELETE http://localhost:8081/api/1.0/entry yields undefined from the routing logger, I'm using express to register the routes. But it looks like I can't even resolve the url / verb.

This is how I'm invoking it:

rows.find('a.remove').on('click', function(){
    $.ajax({
        url: '/api/1.0/entry',
        type: 'DELETE'
    }).done(function(res){
        var row = $(this).parentsUntil('tbody');
        row.slideUp();
    });
});

Sample log

GET / 200 18ms  
GET /author/entry 200 10ms  
GET /api/1.0/entry 200 2ms  
GET /api/1.0/entry 200 1ms  
GET /api/1.0/entry 200 1ms  
undefined

解决方案

Hopefully this can help:

  • Enable logging as your first middleware to make sure the request is coming in:

    app.use(express.logger());

  • Use the methodOverride() middleware:

    app.use(express.bodyParser());

    app.use(express.methodOverride()); // looks for DELETE verbs in hidden fields

  • Create a .del() route:

    app.del('/api/1.0/entry', function(req, res, next) { ... });

这篇关于Node.JS中的HTTP DELETE动词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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