简体   繁体   English

put 在 express js 中不起作用

[英]put is not working in express js

I have POST and PUT that is referring to same FORM.我有 POST 和 PUT 指的是同一个 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 ?当我点击提交按钮时,只有 POST 请求正在被处理(因为 POST 和 PUT 都有相同的路由名称,这是 FORM 的一个动作),我该如何实现 PUT ?

//App.js //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 // newClassifieds.pug

// Method and action of FORM

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

//routes.js ROUTES //routes.js 路由

// Save classified -- POST // 保存分类 -- POST

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

// PUT -- Update classified // PUT -- 更新分类

functions.updateClassified = function (req, res) {

};

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:您必须知道 HTML5 只允许在表单中使用 GET 和 POST,因此如果您必须实现 PUT 操作,您可以按照以下步骤操作:

How can I implement PUT?如何实现 PUT? 1.-Install "method-override" npm package. 1.-安装“方法覆盖”npm 包。 2.-You have to call in your app.js like this: 2.-你必须像这样调用你的 app.js:

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

3.-Add '_method=PUT' in your action form like this: 3.-在您的操作表单中添加“_method=PUT”,如下所示:

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

let me know if you need anything else如果您需要其他任何东西,请告诉我

1)Install method-override package npm install method-override require package in index.js \\code 1)安装方法覆盖包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 \\add _method=put 形式为 like

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM