1

I have a nodeJS server using Express. It uses a pug template. It displays a bunch of images. I want it to display a button on each image. When that button is pressed, the server deletes the image and the client refreshes.

here is what I have so far for the .pug:

// register form
block content
form(name="getomdb",method='post')
    div.input
        input(type="submit",name="delete", value="Delete " + link[0])

    div.container
        h3#title
            p#plot

link[0] references the image I want to delete. As far as I can tell, what this is doing is sending a POST request to my server with the value "Delete " + link[0].

My server attempts to handle it:

    app.post('/', function(req, res){
        console.dir(req.body);
        console.log('post got');
        res.render('index.pug', { links: links})
});

Apparently, there is no body to the request (it prints undefined). How I access which specific link it is trying to delete?

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78

1 Answers1

0

You should be able to access your value through

req.body.delete

Just add the name of the value you are passing in. Keep in mind if you ever start passing in json or other formats, you'll need to check out the "BodyParser" module.

    app.post('/', function(req, res){
        console.dir(req.body.delete); //add the .delete
        console.log('post got');
        res.render('index.pug', { links: links})
});
Spencel
  • 80
  • 1
  • 9