user8765531
user8765531

Reputation:

put is not working in express js

I have POST and PUT that is referring to same FORM. when i click submit button only POST requesting is being processed( because both POST and PUT have got the same route name which is a action of FORM), How can I implement PUT ?

//App.js

app.post('/addClassified',routes().saveClassified);  -- POST
app.put('/addClassified',routes().updateClassified); -- PUT

app.get('/newClassified',function(req,res){
    res.render('newClassifieds'); //Rendering form
});

// newClassifieds.pug

// Method and action of FORM

form(method='POST' action='/addClassified')
    button.btn.btn-primary(type='submit') Save

//routes.js ROUTES

// Save classified -- POST

functions.saveClassified = function (req, res) {
     console.log(req.body.category);
};

// PUT -- Update classified

functions.updateClassified = function (req, res) {

};

Upvotes: 2

Views: 2378

Answers (2)

Surendra Kanigiri
Surendra Kanigiri

Reputation: 11

1)Install method-override package npm install method-override require package in index.js \code

var methodOverride = require("method-override"); app.use(methodOverride("_method")) app.put("/edit",function(req,res){});

\add _method=put in form of like

 <form action="/edit/?_method=PUT" method="post" >

Upvotes: 1

lbegazo
lbegazo

Reputation: 51

You must know that HTML5 allows only GET and POST in forms so if you have to implement PUT actions you could follow these steps:

How can I implement PUT? 1.-Install "method-override" npm package. 2.-You have to call in your app.js like this:

`var methodOverride = require("method-override");
app.use(methodOverride("_method"))`

3.-Add '_method=PUT' in your action form like this:

form(method='POST' action='/addClassified/<%=thing._id%>?_method=PUT')

let me know if you need anything else

Upvotes: 4

Related Questions