对象#<incomingmessage>没有方法'post'</incomingmessage>

时间:2015-01-20 02:17:50

标签: javascript node.js express

因此,此消息接缝与其他方法相同但无法找到我的问题。不知道问题是什么......我知道订单对于快递很重要,但我是新来表达的,这是一个陡峭的学习曲线。提前谢谢。

这是我的错误:

TypeError: Object #<IncomingMessage> has no method 'post'
at module.exports (/home/szpok/Desktop/test/routes/routes.js:14:7)
at Layer.handle [as handle_request] (/home/szpok/Desktop/test/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/szpok/Desktop/test/node_modules/express/lib/router/index.js:269:13)
at /home/szpok/Desktop/test/node_modules/express/lib/router/index.js:236:9
at Function.proto.process_params (/home/szpok/Desktop/test/node_modules/express/lib/router/index.js:311:12)
at /home/szpok/Desktop/test/node_modules/express/lib/router/index.js:227:12
at Function.match_layer (/home/szpok/Desktop/test/node_modules/express/lib/router/index.js:294:3)
at next (/home/szpok/Desktop/test/node_modules/express/lib/router/index.js:188:10)
at Object.<anonymous> (/home/szpok/Desktop/test/node_modules/express-session/index.js:421:7)
at Object.immediate._onImmediate (timers.js:372:16)

这是我在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 mongoose = require('mongoose');
var passport = require('passport');
var session = require('express-session')
var LocalStrategy = require('passport-local').Strategy;

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

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(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(express.bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser('your secret here'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true
}));

// passport config
var Account = require('./models/accounts');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());

// mongoose
mongoose.connect('mongodb://localhost/test');


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


module.exports = app;

这是我在routes.js文件中的代码:

var passport = require('passport');
var Account = require('../models/accounts');

module.exports = function (app) {

  app.get('/', function (req, res) {
      res.render('index', { user : req.user });
  });

  app.get('/register', function(req, res) {
      res.render('register', { });
  });

  app.post('/register', function(req, res) {
    Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) {
        if (err) {
            return res.render('register', { account : account });
        }

        passport.authenticate('local')(req, res, function () {
          res.redirect('/');
        });
    });
  });

  app.get('/login', function(req, res) {
      res.render('login', { user : req.user });
  });

  app.post('/login', passport.authenticate('local'), function(req, res) {
      res.redirect('/');
  });

  app.get('/logout', function(req, res) {
      req.logout();
      res.redirect('/');
  });

  app.get('/ping', function(req, res){
      res.send("pong!", 200);
  });

};

1 个答案:

答案 0 :(得分:1)

app.use('/', routes);

这一行意味着“使用routes作为中间件函数” - 这意味着routes将传递req / res对象和回调函数。

但是您的routes函数期望通过快速应用程序。而不是上述内容,您应该致电:

routes(app)

允许routes函数在快递应用程序中按照自己的意愿设置路由。