nodejs表单发布请求不起作用

时间:2017-07-29 14:01:28

标签: html node.js post jade4j

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

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);
});

请你帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

multipart/form-data不支持

bodyparser,因此您必须使用multer之类的中间件来支持form-data,或者您可以将内容类型更改为{ {1}}。 This帖子会对您有所帮助。