【发布时间】:2021-07-15 11:07:39
【问题描述】:
我想获取后查询值以在数据库中创建新行。但是在 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 请求。 如果有人知道请回复。会有帮助的。
非常感谢
【问题讨论】:
-
请包括您的请求,例如作为
curl。 -
我不明白。实际上我是这门语言的新手,也是这个网站的新手
-
显示客户端代码
-
我更新了问题。请检查一下