Use delete method with express

1
Hello, I'm trying to send a value of a form (id) that will be handled by the expression (I do not know the technical terms for this, if you can help me too), I've tried a lot but I can not pass the id parameter on action and do not access the app.delete () function. How do I pass this to my app.delete?

My form:

 <form  action=<"/produtos">  method="DELETE">
     <input id="id" type="text" name="id"/>
     <input type="submit"  value="REMOVER"/>
 </form>

My app.delete (I was taking Alura course):

app.delete('/produtos:id', function(req, res){
    console.log(req.params.id);
    var connection = app.infra.connectionFactory();
    var ProdutosDAO = new app.infra.ProdutosDAO(connection);
    var produtoID = req.params.id;
    console.log(produtoID);
    ProdutosDAO.remove(produtoID, function(erros, resultados){
      if(erros) res.send(erros.message);
      res.send("Produto " + produtoID + " removido");
    });
  });
    
asked by anonymous 16.08.2017 / 20:34

1 answer

1

Notice that you have a syntax error in HTML, < and > more. It should be <form action="/produtos" method="DELETE">

To facilitate, you use a middleware to receive these parameters in .body of the request. One of the most common (formerly part of Expree.js) is body-parser . If you still do not have it, put it in the application index, right after const app = express(); like this:

const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

Then to receive in the expression parameters passed by the form you can use req.body.id like this:

app.delete('/produtos', function(req, res){
    console.log(req.body.id);
    // etc...
});
    
16.08.2017 / 20:44