vitalym
vitalym

Reputation: 893

Node.js: make delete request with link

I'm trying to send DELETE request with link, so that I can use express.js routing after it

<md-button href="/delete/{{userId}}">Delete</md-button>

app.delete('/delete/:user_id', function(req, res) {
    User.remove({
        _id : req.params.user_id
    }, function(err, user) {
        if (err)
            res.send(err);
        res.redirect('/');
    });
});

Note: md-button comes from https://material.angularjs.org and with href attribute it behaves like anchor tag.

However, it throws an error:

Cannot GET /remove/5536d672106a3b540e0b0d96

Can I change link default behavior to make DELETE request instead of GET, or the only way will be to make an AJAX call and perform routing elsewhere?

Upvotes: 3

Views: 1357

Answers (1)

mkoryak
mkoryak

Reputation: 57938

yes, you will need to make an ajax call where the method is DELETE. clicking on a link will always perform a get request, unless you intercept it with javascript do something else and preventDefault() on the event.

since you are using angular, you could do something like this instead:

<md-button ng-click="vm.deleteUser(userId)">Delete</md-button>

and in your controller (which you bound as 'vm' for this example to work) you can have

this.deleteUser = function(userId){
   ... using your favorite lib do the delete request at '/delete/'+userId
}

Upvotes: 1

Related Questions