在表单上提交POST请求时,继续获取未定义的正文

时间:2015-11-07 09:28:41

标签: node.js

正如标题所说,我已经在这里工作了大约3个小时试图找出为什么POST主体总是未定义 - 无论我做什么。任何人都可以看看我的JADE / JS并帮我弄清楚我的问题吗?

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

1 个答案:

答案 0 :(得分: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;
相关问题