简体   繁体   中英

How to route delete requests in express.route()?

How do I route to the additional '/:task' part in delete() after '/todo' defined in app.route('/todo')

app.route('/todo')
  .get(function(req,res){
      res.render('todo',{todos:data});
   })
   .post(function(req,res){
      data.push(req.body);
      res.json(data);
   })
   .delete('/todo/:task',function(req,res){
      data = data.filter(function(task){
        return task.item.replace(/ /g,'-')!==req.params.task;
      });
      res.json(data);
    })

/:task is the part of the route that accepts a querystring parameter, which is essentially a variable. To route to it, you could do something like this:

fetch('/todo/myvalue')

Where myvalue could be any value you need to send to the server, such as an id for a record you want to delete, or in this case, some task string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM