[英]handling multiple POST requests
with EXPRESS.JS i wrote a service to connect my app to a database, this service is basically an endpoint and inside i have multiple GET or POST requests. 我使用EXPRESS.JS编写了将我的应用程序连接到数据库的服务,该服务基本上是一个端点,并且在我内部有多个GET或POST请求。
now i have to make two different POST request on the same address. 现在我必须在同一地址上进行两个不同的POST请求。
first POST request: 第一个POST请求:
app.post("/accesso", function(req, res) {
connection.getConnection(function(err, connection) {
let sql = "DELETE FROM accesso where ?";
let variabile = { idaccesso: req.body.idaccesso };
connection.query(sql, variabile, function(error, results) {
if (error) throw error;
res.send(results).end();
});
connection.release();
});
});
second POST request: 第二个POST请求:
app.post("/accesso", function(req, res) {
connection.getConnection(function(err, connection) {
let sql = "INSERT INTO accesso SET ?";
let variabile = { utente: req.body.utente };
connection.query(sql, variabile, function(error, results) {
if (error) throw error;
// RISPOSTA DATABASE:
res.send(results).end();
});
connection.release();
});
});
when i test those requests obviously i can't make the post request, because basically the sql query and the body is different. 当我测试这些请求时,显然我无法发出发布请求,因为基本上sql查询和主体是不同的。
is there a way to make multiple POST request on the same TABLE? 有没有办法在同一个表上发出多个POST请求?
Use the next()
middleware. 使用
next()
中间件。
The next middleware function is commonly denoted by a variable named next. 下一个中间件功能通常由名为next的变量表示。
Middleware functions can perform the following tasks: 中间件功能可以执行以下任务:
So, in your code, change your first post request by including the next
parameter and call it once the post is made like this, 因此,在您的代码中,通过添加
next
参数来更改您的第一个发帖请求,并在发帖完成后调用它,
app.post("/accesso", function(req, res, next) {
connection.getConnection(function(err, connection) {
let sql = "DELETE FROM accesso where ?";
let variabile = { idaccesso: req.body.idaccesso };
connection.query(sql, variabile, function(error, results) {
if (error) throw error;
res.send(results).end();
});
connection.release();
next();
});
});
Then place your second post request as is (without any change). 然后按原样放置第二个帖子请求(不做任何更改)。
Hope this helps! 希望这可以帮助!
As your first request want to delete something from DB, you can define a app.delete
method with the same path. 当您的第一个请求要从数据库中删除某些内容时,可以定义具有相同路径的
app.delete
方法。
And let the second method be the same as post
. 并且让第二种方法与
post
相同。
app.delete("/accesso", function(req, res) {
connection.getConnection(function(err, connection) {
let sql = "DELETE FROM accesso where ?";
let variabile = { idaccesso: req.body.idaccesso };
connection.query(sql, variabile, function(error, results) {
if (error) throw error;
res.send(results).end();
});
connection.release();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.