I am learning to code using express js but I encountered a problem today, le me show you some code:
(我正在学习使用Express JS进行编码,但今天遇到了一个问题,请教给我一些代码:)
//main js file
const express=require('express');
const app=express();
const path=require('path');
const dirRoot =require('./util/path')
const bodyParser=require('body-parser');
const adminData = require('./routes/admin.js');
const shopRoute= require('./routes/shop.js');
app.use(express.static(path.join(__dirname , 'public')));
app.use(bodyParser.urlencoded({extended:false}));
app.use('/admin',adminData.routes);
app.use(shopRoute);
app.listen(2000);
//----------------
//admin.js
const products=[];
router.get('/add-product',(req,res,next)=>{
res.sendFile(path.join(dirRoot, 'views', 'home.html'));
//<form action="/admin/add-product" method="post" >
// <input name ="title" type="text">
// <button>Send</button>
// </form> <-- this form is contained in home.html
});
router.post('/add-product',(req,res,next)=>{
products.push({title:req.body.title});
res.redirect('/');
});
exports.routes=router;
exports.products=products;
//-----------------------
//shop.js
const adminData=require('./admin.js');
router.get('/',(req,res,next)=>{
console.log(adminData.products);
res.sendFile(path.join(dirRoot,'views','success.html'))
});
module.exports=router;
Everything seems to work properly but in the console I am not being able to display the user text input, I get just [ { title: undefined } ].
(一切似乎都正常运行,但是在控制台中我无法显示用户文本输入,我只得到了[{title:undefined}]。)
I also tried removing the .title specification from req.body, and it looks like this: [ { title: [Object: null prototype] { inserimento: 'bedduxsdsdsds' } } ]
(我还尝试了从req.body中删除.title规范,它看起来像这样:[{title:[Object:null prototype] {inserimento:'bedduxsdsdsdsds'}}])
in eiher ways there is something wrong that I am doing, could anyone spot what is being incorrect?
(以某种方式,我在做错事,有人可以找出不正确的地方吗?)
Thanks in advance.
(提前致谢。)
ask by Marc Abdel Wahed translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…