Node.js Body-Parser无法检索表单input

我正在试图从一个窗体的inputconsole.log。 但是,正文parsing器显示“未定义”的input,我试图在我的terminal上打印。 我不知道我做了什么不对。 请告知我做错了什么。 谢谢。

HTML表单

<html> <header> <link rel="stylesheet" type="text/css" href="css/form.css"> </header> <body> <form id="form" class="topBefore" action="/form" method="post" enctype="multipart/form-data"> <input id="name" name="name" type="text" placeholder="NAME"> <input id="email" name="email" type="text" placeholder="E-MAIL"> <textarea id="message" name ="message" type="text" placeholder="MESSAGE"></textarea> <input type="file" name="upload" multiple> <input id="submit" type="submit" value="GO!"> </form> </body> </html> 

App.js

 // port and modules var port = 3000; var express = require('express'); var http = require('http'); var path = require('path'); var formidable = require('formidable'); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); var fs = require('fs'); var app = express(); // static resource folder (public) app.use(express.static(path.join(__dirname, 'public'))); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.get('/form', function(req, res){ res.sendFile(__dirname + "/public/form.html"); }); app.post('/form', function(req, res) { //get form input var name = req.body.name; var email = req.body.email; var message = req.body.message; console.log(req.body.name); //upload file and save to directory var form = new formidable.IncomingForm(); form.parse(req); //set upload path form.on('fileBegin', function (name, file){ file.path = __dirname + '/public/icons/' + file.name; console.log(file.path); }); //upload process form.on('file', function (name, file){ console.log('Uploaded ' + file.name); }); // redirect to home page after form submission res.redirect('/'); }); // connect to server and listen port // index.html will be home page http.createServer(app).listen(port, function(){ console.log("{ server connection : success }"); }); //Database setup mongoose.connect('mongodb://localhost/mydb'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { console.log('{ database connection : success}'); }); 

       

网上收集的解决方案 "Node.js Body-Parser无法检索表单input"

如果您正在使用节点中的文件,bodyParser将不起作用。

您的表单需要以下属性

 <form action="/account/edit" method="post" enctype="multipart/form-data"></form> 

在节点中,您需要请求分割

 var multer = require('multer'); 
 // this is a simple reqex filter to check the ext const imageFilter = (req, file, cb) => { let regex = new RegExp(/\.(jpg|jpeg|png|gif)$/, "i"); // return error if (!file.originalname.match(regex)) { return cb(new Error('Only image files are allowed!'), false); } cb(null, true); }; // define the destination var storage = multer.diskStorage({ destination: function(req, file, cb) { pathName = `photos/users/${req.user.dataValues.userid}`; var p = mkdirSync(pathName) // checks if photos/users exist if (p === false) { cb(true, null) // path does not exist } else { cb(null, pathName) // path exist } }, filename: function(req, file, cb) { let regex = new RegExp(/\.(jpg|jpeg|png|gif|bmp)$/, "i"); let filename = file.originalname; let ext_arr = filename.match(regex); let ext_str = ext_arr[0]; // get ext cb(null, `${Date.now()}${ext_str}`); // file name is date.ext } }) // if no directory, make directory const mkdirSync = (dirPath) => { try { fs.mkdirSync(dirPath) // try it without making anything } catch (err) { if (err.code !== 'EEXIST') { fs.mkdirSync("photos/users") // make directory try { fs.mkdirSync(dirPath) // try it now that users directory is made } catch (err) { if (err.code !== 'EEXIST') { return false; } } } } } // makes the directory const checkUserdirSync = (dirPath) => { try { fs.mkdirSync(dirPath) } catch (err) { if (err.code !== 'EEXIST') throw err } }