Skip to main content
Los snippets son sólo para código que se puede ejecutar directamente en la respuesta, que no es el caso de nodejs. Para el resto, usar el botón "{ }"
Origen Enlace
Mariano
  • 24.5k
  • 20
  • 76
  • 105

¿Como ¿Cómo agregar métodonométodo put y delete?

Hola tengoTengo esta aplicación hecha en nodejs, necesito. Necesito agregar el método put pero no me deja y no se donde estaestá el error

  1. Aqui el código del ruter:
router.get("/post/:title", function(req, res){
    var query = {"title" : req.params.title};
    post.findOne(query, function(err, posts){
        res.render("admin/posts/posts", {posts : posts});
    });
});

router.put('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  var update = {title: req.body.title, body : req.body.post, created_at : req.body.date, autor: req.body.autor};
  var options = {new: true};
  post.findOneAndUpdate(query, update, options, function(err, posts){
    console.log(posts)
    res.render(
      'admin/posts/post',
      {title : 'posts - ' + posts.pregunta, posts : posts}
    );
  });
});

router.delete('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  post.findOneAndRemove(query, function(err, posts){
    console.log(posts)
    res.redirect('/admin/posts/');
  });
});
router.get("/post/:title", function(req, res){
    var query = {"title" : req.params.title};
    post.findOne(query, function(err, posts){
        res.render("admin/posts/posts", {posts : posts});
    });
});

router.put('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  var update = {title: req.body.title, body : req.body.post, created_at : req.body.date, autor: req.body.autor};
  var options = {new: true};
  post.findOneAndUpdate(query, update, options, function(err, posts){
    console.log(posts)
    res.render(
      'admin/posts/post',
      {title : 'posts - ' + posts.pregunta, posts : posts}
    );
  });
});

router.delete('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  post.findOneAndRemove(query, function(err, posts){
    console.log(posts)
    res.redirect('/admin/posts/');
  });
});
2.codigo de la vista hbs
<form  method="post" action="/admin/post/{{posts.title}}?_method=PUT">
    titulo
    <input type="text" name="title" value="{{posts.title}}">
    entrada
    <input type="text" name="body" value="{{posts.post}}">
    fecha
    <input type="date" name="date" value="{{posts.created_at}}">
    autor
    <input type="text" name="autor" value="{{posts.autor}}">
    <button type="submit">Enviar</button>
</form>
<form method="post" action="/admin/post/{{posts.title}}?_method=DELETE">
    <button type="submit">Delete</button>
</form>
2.codigo de la vista hbs

 
<form  method="post" action="/admin/post/{{posts.title}}?_method=PUT">
    titulo
    <input type="text" name="title" value="{{posts.title}}">
    entrada
    <input type="text" name="body" value="{{posts.post}}">
    fecha
    <input type="date" name="date" value="{{posts.created_at}}">
    autor
    <input type="text" name="autor" value="{{posts.autor}}">
    <button type="submit">Enviar</button>
</form>
<form method="post" action="/admin/post/{{posts.title}}?_method=DELETE">
    <button type="submit">Delete</button>
</form>

¿Como agregar métodono put y delete?

Hola tengo esta aplicación hecha en nodejs, necesito agregar el método put pero me deja y no se donde esta el error

  1. Aqui el código del ruter
router.get("/post/:title", function(req, res){
    var query = {"title" : req.params.title};
    post.findOne(query, function(err, posts){
        res.render("admin/posts/posts", {posts : posts});
    });
});

router.put('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  var update = {title: req.body.title, body : req.body.post, created_at : req.body.date, autor: req.body.autor};
  var options = {new: true};
  post.findOneAndUpdate(query, update, options, function(err, posts){
    console.log(posts)
    res.render(
      'admin/posts/post',
      {title : 'posts - ' + posts.pregunta, posts : posts}
    );
  });
});

router.delete('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  post.findOneAndRemove(query, function(err, posts){
    console.log(posts)
    res.redirect('/admin/posts/');
  });
});
2.codigo de la vista hbs
<form  method="post" action="/admin/post/{{posts.title}}?_method=PUT">
    titulo
    <input type="text" name="title" value="{{posts.title}}">
    entrada
    <input type="text" name="body" value="{{posts.post}}">
    fecha
    <input type="date" name="date" value="{{posts.created_at}}">
    autor
    <input type="text" name="autor" value="{{posts.autor}}">
    <button type="submit">Enviar</button>
</form>
<form method="post" action="/admin/post/{{posts.title}}?_method=DELETE">
    <button type="submit">Delete</button>
</form>

 

¿Cómo agregar método put y delete?

Tengo esta aplicación hecha en nodejs. Necesito agregar el método put pero no me deja y no se donde está el error

  1. Aqui el código del ruter:
router.get("/post/:title", function(req, res){
    var query = {"title" : req.params.title};
    post.findOne(query, function(err, posts){
        res.render("admin/posts/posts", {posts : posts});
    });
});

router.put('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  var update = {title: req.body.title, body : req.body.post, created_at : req.body.date, autor: req.body.autor};
  var options = {new: true};
  post.findOneAndUpdate(query, update, options, function(err, posts){
    console.log(posts)
    res.render(
      'admin/posts/post',
      {title : 'posts - ' + posts.pregunta, posts : posts}
    );
  });
});

router.delete('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  post.findOneAndRemove(query, function(err, posts){
    console.log(posts)
    res.redirect('/admin/posts/');
  });
});
2.codigo de la vista hbs

<form  method="post" action="/admin/post/{{posts.title}}?_method=PUT">
    titulo
    <input type="text" name="title" value="{{posts.title}}">
    entrada
    <input type="text" name="body" value="{{posts.post}}">
    fecha
    <input type="date" name="date" value="{{posts.created_at}}">
    autor
    <input type="text" name="autor" value="{{posts.autor}}">
    <button type="submit">Enviar</button>
</form>
<form method="post" action="/admin/post/{{posts.title}}?_method=DELETE">
    <button type="submit">Delete</button>
</form>
Origen Enlace
awamas
  • 146
  • 1
  • 8

¿Como agregar métodono put y delete?

Hola tengo esta aplicación hecha en nodejs, necesito agregar el método put pero me deja y no se donde esta el error

  1. Aqui el código del ruter

router.get("/post/:title", function(req, res){
    var query = {"title" : req.params.title};
    post.findOne(query, function(err, posts){
        res.render("admin/posts/posts", {posts : posts});
    });
});

router.put('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  var update = {title: req.body.title, body : req.body.post, created_at : req.body.date, autor: req.body.autor};
  var options = {new: true};
  post.findOneAndUpdate(query, update, options, function(err, posts){
    console.log(posts)
    res.render(
      'admin/posts/post',
      {title : 'posts - ' + posts.pregunta, posts : posts}
    );
  });
});

router.delete('/post/:title', function(req, res) {
  var query = {"title": req.params.title};
  post.findOneAndRemove(query, function(err, posts){
    console.log(posts)
    res.redirect('/admin/posts/');
  });
});
2.codigo de la vista hbs
<form  method="post" action="/admin/post/{{posts.title}}?_method=PUT">
    titulo
    <input type="text" name="title" value="{{posts.title}}">
    entrada
    <input type="text" name="body" value="{{posts.post}}">
    fecha
    <input type="date" name="date" value="{{posts.created_at}}">
    autor
    <input type="text" name="autor" value="{{posts.autor}}">
    <button type="submit">Enviar</button>
</form>
<form method="post" action="/admin/post/{{posts.title}}?_method=DELETE">
    <button type="submit">Delete</button>
</form>