1

我正在尝试构建节点应用程序的登录,尝试访问路由/登录获取:req.body undefined

错误:

TypeError: Cannot read property 'usuario' of undefined
   at login (/home/makros/workspace/ntalk/controllers/home.js:8:24)
   at Layer.handle [as handle_request] (/home/makros/workspace/ntalk/node_modules/express/lib/router/layer.js:82:5)
   at next (/home/makros/workspace/ntalk/node_modules/express/lib/router/route.js:100:13)
   at Route.dispatch (/home/makros/workspace/ntalk/node_modules/express/lib/router/route.js:81:3)
   at Layer.handle [as handle_request] (/home/makros/workspace/ntalk/node_modules/express/lib/router/layer.js:82:5)
   at /home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:235:24
   at Function.proto.process_params (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:313:12)
   at /home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:229:12
   at Function.match_layer (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:296:3)
   at next (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:190:10)

应用程序.js

var express = require('express'),
load = require('express-load'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
bodyParser = require('body-parser'),
app = express();


load('models')
    .then('controllers')
    .then('routes')
    .into(app);

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.use(cookieParser('secret'));
app.use(session({
    secret: 'secret', 
    resave: true, 
    saveUninitialized: true
}));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

app.listen(3000,function(){
    console.log('Started!');
});

包.json

{
  "name": "ntalk",
  "version": "1.0.0",
  "private": true,
  "scripts": {
  "start": "node ./bin/www"
  },
  "dependencies": {
  "body-parser": "^1.9.0",
  "cookie-parser": "^1.3.3",
  "ejs": "~0.8.5",
  "express": ">= 0.0.0",
  "express-load": "^1.1.14",
  "express-session": "^1.8.2"
  }
}

控制器/home.js

module.exports = function(app) {
    return {
        index: function(req, res) {
            res.render('home/index');
        },
        login: function(req, res){
            console.log(req.params);
            var email = req.body.usuario.email,
                nome = req.body.usuario.nome;
            if(email && nome) {
                var usuario = req.body.usuario;
                usuario['contatos'] = [];
                req.session.usuario = usuario;
                res.redirect('/contatos');
            } else {
                res.redirect('/');
            }
        },
        logout: function(req, res){
            req.session.destroy();
            res.redirect('/');
        }
    };
};

路线/home.js

module.exports = function(app) {
    var home = app.controllers.home;    
    app.get('/', home.index);
    app.get ('/entrar', home.login);
    app.get('/sair', home.logout);
};

意见/主页/index.ejs

<% include ../header %>
        <header>
            <h1>Ntalk</h1>
            <h4>Bem-vindo!</h4>
        </header>
        <section>
            <form action="/entrar" method="post">
                <input type="text" name="usuario[nome]" placeholder="Seu nome">
                <br>
                <input type="text" name="usuario[email]" placeholder="Seu e-mail">
                <br>
                <button type="submit">Entrar</button>
            </form>
        </section>
<% include ../footer %>

请帮我!

4

3 回答 3

1

实际上问题出在我的 express-load 实现中,它注入了依赖项但没有配置应用程序,所以我决定不使用它:

应用程序.js

var express = require('express'),
    path = require('path'),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    bodyParser = require('body-parser');

var routes = require('./routes/home'),
    contacts = require('./routes/contacts');


var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
    secret: 'secret',
    resave: true, 
    saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/contatos', contacts);

app.listen(3000,function(){
    console.log('Started!');
});

路线/home.js

var router = require('express').Router();
var home = require('../controllers/home');

router.get('/', home.index);
router.post('/entrar', home.login);
router.get('/sair', home.logout);
module.exports = router;

控制器/home.js

module.exports = {
    index: function(req, res) {
        /*do somethings*/
    },
    login: function(req, res){
        /*do somethings*/
    },
    logout: function(req, res){
        /*do somethings*/
    }
};

必须有更好的方法来实现使用 express4 的 express-load,但尚未找到。

于 2014-12-11T00:22:33.343 回答
1

尝试移动

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

load('models')
    .then('controllers')
    .then('routes')
    .into(app);
于 2014-12-07T03:14:08.270 回答
1

您正在将您的路由处理程序添加到app您的正文解析中间件之前。所有路由/中间件/等。添加到app在 Express 4 中按顺序执行。

所以移动这个:

load('models')
    .then('controllers')
    .then('routes')
    .into(app);

这一行之后:

app.use(express.static(__dirname + '/public'));

在不相关的注释中,您通常应该将您的express.static()使用放在您的路由/中间件列表的顶部附近以提高效率。例如,这可以防止自动会话创建和对静态资产请求进行不必要的 cookie 解析。

于 2014-12-05T17:01:17.380 回答