2

I don't understand a couple of thing about how to use POST and PUT

first of all if you have a form with method="post" can you use app.put("/path") to change data? I know I could change specific data from req.body.name through using app.post() with mongoose. So why would I need PUT?

Second of all, I noticed that if you usemethod="PUT" on the form and i click submit i get a query string of the data in the url. even if i use res.redirect("/path"), I get the query string and the page doesn't redirect. Is that normal? why do I get the query string? and why doesn't it redirect

app.put("/myaction", function(req, res){
//originally i had it at req.body.name then browser  said that it "cannot POST"
//so I changed the method to PUT so thats why used req.query.name because I got
//the query string. if
name = req.query.name;
  Name.findOneAndUpdate({name : name}, {name : " Itwas changed through PUT "}, function(err, result){
        console.log("result : ", result);
       res.redirect("/result")
  })
})

If im supposed to get these querystring how should i update the database?

Also I've noticed that if you have app.post("/path") and app.put("/path") express will use app.post especially when you have method="post" so should you use put if you want to use the same form if you want to allow the user to change their name if they enter it? in this case the new name would be 'Itwas changed through PUT'

jack blank
  • 5,073
  • 7
  • 41
  • 73

3 Answers3

3

You can send data to server using both http request verb POST and PUT. But usually POST use to create/insert data and PUT use to update data.

if you have a form with method="post" can not use app.put() only can use

app.post().

you can use different request verb(get, post, put, delete) for same path

like:

app.route("/path")
    .post(function(req, res) {})
    .put(function(req, res) {})
    .get(function(req, res) {})
    .delete(function(req, res) {})

actually which function will be calling it depends on what request verb you are using to the path.

if use method='GET' in HTML form or type='GET' in ajax or $http.get() in service then call .get()

if method='POST' in HTML form or type='POST' in ajax or $http.post() in service then call .post()

if method='PUT' in HTML form or type='PUT' in ajax or $http.put() in service then call .put()

if method='DELETE' in HTML form or type='DELETE' in ajax or $http.delete() in service then call .delete()

For more details read this article

Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • Thanks for your answer I think I'm learning something new today. You say i can't use app.put() with `method="post"`. the only other method is get so is it safe to say that you could only use .put() when the method value is get on the form? – jack blank Aug 15 '15 at 20:51
  • Edited my post after your comment – Shaishab Roy Aug 16 '15 at 09:33
1

POST and PUT are used for sending data to a server. How you use them is up to you, but generally POST is used to inserts of data and PUT is used for updating data

Info about restful services

Wout De Rooms
  • 647
  • 4
  • 10
  • Thanks for your answer but I was aware of that information. Can you tell why when I use put and submit the form I get a query string in the url? – jack blank Aug 15 '15 at 14:17
1

HTML is only supporting post and get as methods.

<form method="post"></form>

or

<form method="get"></form>

You can only use the other [verbs][1](inclusive get and post) with javascript.

For example with jquery http://api.jquery.com/jquery.ajax/

Why do developers use multiple verbs in their application?

It makes more sense to you if you do name your routes with nouns than actions.

/books - GET - get the collection of books

/books - POST - insert into the collection

/book/:id - GET - get the book with :id

/book/:id - PUT - update the book with :id

/book/:id - DELETE - delete the book with :id

Can you tell why when I use put and submit the form I get a query string in the url?

You use req.body when you send requests with the methods POST,PUT,DELETE to the server. Because you send your requests with javascript(client-side) you set your values with the property data (see below) for req.body on your server.

//CLIENT
$.ajax({
  method: "POST",
  url: "/books",
  data: { name: "Some random title"}
})
  .done(function( msg) {
    alert( "Data inserted: " + msg );
  });



//SERVER
app.post('/books',function(req,res){
    console.log(req.body.name);
}

PS: If you want to use req.body you need a middleware called body-parser https://www.npmjs.com/package/body-parser

Server Setup:

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
kper
  • 313
  • 3
  • 12
  • have used app.put("/path") to update a data in a DB recently? my problem is that i get a query string in the url with the updated data. I want to know if that is normal behavior. so if you could test it out and confirm if you get the string that will be really appreciated. if you don't get the string or if you were able to update to mongodb explain how you did it. I know what the HTTP methods do and I have been using body-parser. Thanks for answering about the html. To confirm. no one use `
    ` .so i should't try that any more if I want to avoid making errors
    – jack blank Aug 15 '15 at 18:21
  • Ok. I did test this out. If you use method="put" it does not recognize it, and HTML automatically sends his request with method="get" out. Normal behaviour of GET is to append it to the URL. Why does it not redirect? As above explained the client sends his request with GET (even method="put" except method="post" or method="get", because it only knows GET,POST) so your server route is invalid and do not fire it and therefore do not redirect. You need to change: `app.put("/myaction", function(req, res){` to: `app.get("/myaction", function(req, res){` that your route matches. – kper Aug 16 '15 at 07:56
  • But mind that you're doing your updates with GET. The better approach would be to do your request with javascript. http://stackoverflow.com/questions/921942/javascript-rest-client – kper Aug 16 '15 at 08:19
  • thanks for your effort. I'll look this over tomorrow and prob accept this answer when I'm done and if I have any questions I'll let you know. – jack blank Aug 16 '15 at 08:22