[英]On post request req.query returns empty dictionary in express node
我想獲取后查詢值以在數據庫中創建一個新行。 但是在 req.query 中它不起作用。 我也嘗試過使用 body-parser,但它也沒有奏效。 編碼
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './static/image/')
},
filename: (req, file, cb) => {
const { originalname } = file;
cb(null, originalname)
filename = originalname;
}
})
const upload = multer({ storage: storage })
app.post('/admin/song/create', upload.single('file'), async(req, res) => {
console.log(req.query)
res.redirect('/admin')
})
和客戶端代碼
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>Document</title> <link href="https.//cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min:css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> <link rel="stylesheet" href="https.//pro.fontawesome.com/releases/v5.10.0/css/all:css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/> </head> <body> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <style> input{ margin; 10px; } </style> <div class="container" id="conT"> <form action="/admin/song/create" method="POST" enctype="multipart/form-data"> <input type="file" name="file" class="btn-success btn"> <input type="text" placeholder="Name" name="name" > <input type="text" placeholder="Movie Or Album Name" name="movie_or_album_name" > <input type="text" placeholder="Description" name="description" > <input type="text" placeholder="URL" name="urls" > <input type="number" placeholder="year" name="description" > <button type="submit" class="btn-success btn">Submit</button> </form> </div> </body> </html>
而且我也確實為 body-parser 設置了中間件,但它也不起作用。 但它確實適用於 GET 請求。 為什么它不適用於 POST 請求。 如果有人知道請回復。 這會很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.