当我在 add-product.pug 文件中填写表单中的信息时,该信息应该在主页上显示为卡片,并且应该作为重定向返回到“/”目录,但它不会发生。 当我点击提交按钮时,我剪切了一些部分,但没有任何反应
添加产品.pug 表单
form(action='/admin/add-product', method='POST')
.form-group.row.my-3
label.col-sm-2.col-form-label(for='name') Product Name
.col-sm-10
input.form-control#name(type='text', name='name')
.form-group.row
.col-sm-10.offset-sm-2.my-3
input(type="submit" value="Submit").btn.btn-primary
admin.js
const products = [
{name : 'Samsung S8', price: 3000, image: 'image1.jpeg', description:'iyi telefon'},
{name : 'Samsung S7', price: 3000, image: 'image2.jpeg' ,description:'idare eder'},
{name : 'Samsung S6', price: 3000, image: 'image3.jpeg' ,description:'çok iyi'},
{name : 'Samsung S5', price: 3000, image: 'image4.jpeg' ,description:'güzel telefon'},
]
router.get('/add-product', (req, res, next) => {
res.render('add-product',{title:'Add a New Product'});
});
router.post('/add-product', (req, res, next) => {
products.push({name:req.body.name,price:req.body.price,image:req.body.image,description:req.body.description});
console.log(req.body);
res.redirect('/');
});
exports.routes=router;
exports.products=products;
用户.js
const express = require('express');
const router = express.Router();
const admin= require('./admin');
router.get('/', (req, res, next) => {
res.render('index', { title: 'Homepage', products: admin.products });
});
module.exports = router;
app.js
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser')
app.set('view engine','pug');
app.set('views','./views');
const userRoutes = require('./routes/user');
const admin = require('./routes/admin');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname,'public')));
app.use('/admin', admin.routes);
app.use(userRoutes);
app.listen(3000, () => {
console.log("Listening on port 3000");
});
我不确定,但看起来 .form-group 与表单本身处于同一级别。
这是真的还是只是看起来像这样? 表单组应位于表单内部。
form(action='/admin/add-product', method='POST')
.form-group.row.my-3
label.col-sm-2.col-form-label(for='name') Product Name
.col-sm-10
input.form-control#name(type='text', name='name')
.form-group.row
.col-sm-10.offset-sm-2.my-3
input(type="submit" value="Submit").btn.btn-primary