想在本机模拟一下post请求,但是一直500,err是type is not define,求助
发布于 1 个月前 作者 striderdu 274 次浏览 来自 问答
//路由
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('user/index', { title: 'Express' });
});

router.get('/register',function(req,res,next){
  res.render('user/register' ,{title: 'Register'});
});

router.post('/register', function(req,res){  
  var user_type=req.body.user-type;
  var email=req.body.email; 
  var password=req.body.pswd;
  console.log("User type = "+user_type+", email = "+email+", password is "+password);  
  res.end("yes");  
});

//app.js
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 user = require('./routes/controller/usercontroller');
var org = require('./routes/controller/orgcontroller')
var contest = require('./routes/controller/contestcontroller')

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// 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.use('/', routes);
app.use('/user', user);
app.use('/org',org);
app.use('/contest',contest);

// 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: {}
  });
});

module.exports = app;

//前端
extends ../layout/layout_nav
block content
  h1= title
  div.container(style="width:60%")
    form.form(action='/user/register' method="post")
      label contestant or coach?
      br
      div.radio-inline
        input(type="radio" name="user-type" value="contestant")
        p contestant
      div.radio-inline
        input(type="radio" name="user-type" value="coach")
        p coach
      div.form-group
        label email
        input.form-control(type="email" name="email")
      div.form-group
        label password
        input.form-control(type="password" name="pswd")
      div.form-group
        label repeat
        input.form-control(type="password" name="repswd")
      div.form-group
        input.btn-default.btn(type="submit")
6 回复

你还不如直接贴一张报错的图出来

var user_type=req.body.user-type;

没有 body.user-type 这种写法

var user_type = req.body['user-type']

@airyland 感谢…就是这个…

回到顶部