2
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 expressValidator = require('express-validator');

var routes = require('./routes/index');
var users = require('./routes/users');
var auth = require('./routes/auth');

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

//validator
app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
    var namespace = param.split('.')
        , root    = namespace.shift()
        , formParam = root;

    while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param : formParam,
      msg   : msg,
      value : value
    };
  }
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/auth', auth);

// 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;

このセクションは app.js です

var express = require('express');
var router = express.Router();

router.post('/register', function(req,res,next) {

    // Get Form Values

    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;

    // Form Validation
    req.checkBody('email','Email field is required').notEmpty();
    req.checkBody('email','Email not valid').isEmail();
    req.checkBody('username','Username field is required').notEmpty();
    req.checkBody('password','Password field is required').notEmpty();
    req.checkBody('password2','Passwords do not match').equals(req.body.password);


    var errors = req.validationErrors();

    if(errors){
        console.log(req.body);
        console.log(req.param);
        console.log(errors);
    } else{
        res.send('email : ' + email +'username : ' + username + ' password : ' + password);

    }
});



module.exports = router;

このセクションはauth.jsです

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=""width="device-width,initial-scale=1.0"/>
    <meta name="description" content="" />
    <meta name="copyright" content="" />
    <title>project</title>


    <link rel="stylesheet" href="jquery/jquery.mobile-1.4.5.min.css"/>
    <script src="jquery/jquery.min.js"></script>
    <script src="jquery/jquery.mobile-1.4.5.min.js"></script>

</head>

<body>

    <div data-role="page" id = "loginpage">
        <div data-role="header">

        </div>

        <div data-role="content">
        <h1>My Project</h1>
        <form method="post" action="" enctype="application/x-www-form-urlencoded">
            <input type="text" class="username" placeholder="username" />
            <input type="password" class="password" onfocus="this.value = '';" placeholder="password" />
            <input type="submit" class="button" value="LOGIN" />
        </form>
        <a href="#registerpage">Not a member? Signup now</a>
        </div>
    </div>

    <div data-role="page" id="registerpage">
        <div data-role="header">

        </div>
        <div data-role="content">

        <form  method="post" action="http://127.0.0.1:3000/auth/register"  enctype="application/x-www-form-urlencoded">
            <input type="text" class="username" placeholder="Username"/>
            <input type="password" class="password" onfocus="this.value = '';" placeholder="Password" />
            <input type="text" class="email" placeholder="user@project.com"/>
            <input type="password" class="password2" onfocus="this.value = '';" placeholder="Check Password" />
            <input type="submit" class="button" value="Register" />


        </form>
        </div>


    </div>

</body>
</html>

この部分はクライアント部分です

クライアント部分とサーバー部分を別々に開発しています

body-parser が機能しない理由がわかりません

リクエストボディは常に未定義です。

Express バージョン: 4.13.3 body-parser バージョン: 1.14.1

4

0 に答える 0