nodejs 表单 post 请求无效

nodejs form post request is not working

我正在使用 nodejs 和 jade 创建登录表单。在我的第一步中,我想将字段名(id=name,和 name=name)传递给后端。我已经尝试了这两种方法(从表单和 postman 发送 post 请求)但是这两种方法都没有通过在后端请求字段名来响应控制台。我看到的是路由器打开了正确的功能。下面的代码显示了场景:

router.js

app.post('/login/post', require('./views/login/index').login);

login.js

exports.login = function (req, res, next){
//This works
console.info('Login...')
//This does not work
console.info(req.body.name)
//This works
res.render('login/index')}

login.jade

    form(method='post', action='/login/post', enctype='multipart/form-data')
      input#name.form-control(type='text', name='name')
      button#submit.btn.btn-primary.btn-lg.btn-block.login-button(type='submit') Login

APP.JS

//dependencies
var config = require('./config'),
    express = require('express'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    session = require('express-session'),
    mongoStore = require('connect-mongo')(session),
    http = require('http'),
    path = require('path'),
    multer  =   require('multer'),
    passport = require('passport'),
    mongoose = require('mongoose'),
    helmet = require('helmet');


var storage =   multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now());
    }
});

var upload = multer({ storage : storage}).single('userPhoto');

 //   csrf = require('csurf');

//create express app
var app = express();

//keep reference to config
app.config = config;

//setup the web server
app.server = http.createServer(app);

//setup mongoose
app.db = mongoose.createConnection(config.mongodb.uri);
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
});

//config data models
require('./models')(app, mongoose);

//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

//middleware
app.use(require('morgan')('dev'));
app.use(require('compression')());
app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(require('method-override')());
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.cryptoKey));
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: config.cryptoKey,
  store: new mongoStore({ url: config.mongodb.uri })
}));
app.use(passport.initialize());
app.use(passport.session());
//app.use(csrf({ cookie: { signed: true } }));
helmet(app);

//response locals
app.use(function(req, res, next) {
 // res.cookie('_csrfToken', req.csrfToken());
  res.locals.user = {};
  res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl();
  res.locals.user.username = req.user && req.user.username;
  next();
});

//global locals
app.locals.projectName = app.config.projectName;
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.config.companyName;
app.locals.cacheBreaker = 'br34k-01';

//setup passport
require('./passport')(app, passport);

//setup routes
require('./routes')(app, passport);

//custom (friendly) error handler
app.use(require('./views/http/index').http500);

//setup utilities
app.utility = {};
app.utility.sendmail = require('./util/sendmail');
app.utility.slugify = require('./util/slugify');
app.utility.workflow = require('./util/workflow');

//listen up
app.server.listen(app.config.port, function(){
  //and... we're live
  console.log('Server is running on port ' + config.port);
});

请你帮我解决这个问题。

multipart/form-data 不受 bodyparser 支持,因此您必须使用中间件,例如 multer for the support of form-data or you can change the content-type to x-www-form-urlencoded. This post 会对您有所帮助。