2015-11-07 68 views
0

正如標題所說,我一直在這裏工作了大約3個小時,試圖弄清楚爲什麼POST主體總是未定義 - 無論我做什麼。任何人都可以看看我的JADE/JS,並幫我弄清楚我的問題?在表單上提交POST請求時,請保持未定義的身體

JADE

doctype html 
html(lang="en" ng-app) 
    head 
    meta(charset="utf-8") 
    meta(http-equiv="X-UA-Compatible", content="IE=edge") 
    meta(name="viewport", content="width=device-width, initial-scale=1") 
    // The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags 
    meta(name="description", content="") 
    meta(name="author", content="") 
    link(rel="icon", href="favicon.ico") 
    title Signin Template for Bootstrap 
    // Bootstrap core CSS 
    link(href="css/bootstrap.min.css", rel="stylesheet") 
    // Custom styles for this template 
    link(href="css/signin.css", rel="stylesheet") 
    // Just for debugging purposes. Don't actually copy these 2 lines! 
    //if lt IE 9 
     script(src="assets/js/ie8-responsive-file-warning.js") 
    // <script src="assets/js/ie-emulation-modes-warning.js"></script> 
    // HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries 
    //if lt IE 9 
     script(src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js") 
     script(src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js") 
    body 
    .container 
     form.form-signin(method="post", action="/") 
     h2.form-signin-heading(style="text-align:center;") Please sign in 
     label.sr-only(for="inputEmail") Student ID 
     input#inputID.form-control(type="text", name="userID", placeholder="User ID", required="", autofocus="") 
     label.sr-only(for="inputPassword") PIN: 
     input#inputPIN.form-control(type="password", name="userPIN", placeholder="Password", required="") 
     button.btn.btn-lg.btn-primary.btn-block(type="submit") Sign in 
    // /container 
    // IE10 viewport hack for Surface/desktop Windows 8 bug 
    script(src="assets/js/ie10-viewport-bug-workaround.js") 
    //AngularJS CDN 
    script(src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js") 

server.js

//Add necessary dependencies (Express and MongoJS) 
var express = require('express'); 
var app = express(); 
var mongojs = require('mongojs'); 
var db = mongojs('advisingApp',['advisingApp']); //sets the database for the project 
//Test to make sure server is properly configured 
/*app.get('/', function (request, response) { 
    response.send("Hello world from server.js!"); 
});*/ 

//Tell web app where to look for "static" files in directory - (it's looking in the default parent directory) 
app.use(express.static(__dirname)); 

// app.get("/", function (request, response) { 
// console.log("GET Request Received") 
// db.advisingApp.find(function (err, docs) { 
//  console.log(docs); 
//  response.json(docs);   
// }); 
// }); 
app.post("/", function (req, res) { 
    console.log("POST Request Received"); 
    console.log(req.body); 
}); 
app.listen(3000); 
console.log("Server running smoothly on port 3000"); 
+0

試了一下剛纔,做工精細'POST請求收到 {用戶名: '用戶',userPIN :'pass'}' 你可能會缺少這3行 'var bodyParser = requi re('body-parser');' 'app.use(bodyParser.json());' 'app.use(bodyParser.urlencoded({extended:false}));' –

+0

@aishwatsingh that do it!雖然我並不完全理解body-parser的需要,但是在沒有它的情況下解碼POST請求會出現一些問題嗎? –

+0

因爲在post方法中你必須解析body才能使用它,這就是bodyparser所做的事情https://www.npmjs.com/package/body-parser 很自我解釋我猜,你用req.body不是req.prams或req.query。 PLZ接受答案,如果有幫助 –

回答

0

試試這個

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 routes = require('./routes/index'); 
//var mongoose=require('mongoose'); 
//mongoose.connect('mongodb://localhost/test'); 

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: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 


app.get("/",function(req,res){ 
    res.render('view'); //i named ur given jade as view.jade 
}) 
app.post("/", function (req, res) { 
    console.log("POST Request Received"); 
    console.log(req.body); 
}); 
// app.use('/', routes); 
// app.use('/users', users); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handlers 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: err 
    }); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 
// 
app.listen('3000',console.log('listening')); 
// module.exports = app;