gpt4 book ai didi

javascript - Expressjs 模块 bodyparser 返回未定义

转载 作者:太空宇宙 更新时间:2023-11-04 02:21:31 24 4
gpt4 key购买 nike

我正在使用一个简单的 html 表单,尝试解析表单数据。不幸的是,我的路线部分仅从我的表单元素返回未定义

我的app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var busboy = require('connect-busboy');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
module.exports = app;

Jade 质形态,

form(method='post', action='/contact', enctype="multipart/form-data")
.form-group
label Name:
input.form-control(name='name', type='text')
input.btn.btn-default(name='submit',type='submit',value='Save')

我的路线

router.post('/contact', function(req, res, next) {
var name = req.body.name;

console.log(req.body.name);

res.redirect('google.com');
});

最佳答案

您的表单有multipart/form-data编码类型:

form(method='post', action='/contact', enctype="multipart/form-data")

但正如所写的here , body-parser不处理此类主体:

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

或者你应该更改中间件来解析正文,或者你的表单 enctype

关于javascript - Expressjs 模块 bodyparser 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com