2017-07-29 71 views
1

我使用nodejs和jade創建登錄表單。在我的第一步中,我想將字段名(id = name和name = name)傳遞給後端。我已經嘗試了這兩種方法(從窗體和郵遞員發送post請求),但是任一方法都不會通過在後端請求字段名來響應控制檯。我所看到的是路由器打開正確的功能。下面的代碼顯示的場景:nodejs表單發佈請求不起作用

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

請你能幫助我解決這個問題。

+0

你使用'bodyparser'嗎? –

+0

@ArpitSolanki'body-parser'模塊不適用於multipart/form-data。他們將需要使用像'multer'或'強大的'來處理這些形式。 – mscdex

+0

@mscdex這就是爲什麼我問OP使用哪個模塊 –

回答

1

multipart/form-databodyparser支持,所以你必須要麼使用像multer中間件爲支撐的form-data或者您可以更改內容類型x-www-form-urlencodedThis後會對你有所幫助。