gpt4 book ai didi

javascript - 对象 # 没有方法 'post'

转载 作者:行者123 更新时间:2023-12-03 11:02:09 27 4
gpt4 key购买 nike

所以这条消息似乎与其他方法很常见,但找不到它来解决我的问题。不知道问题是什么......我知道顺序对于 express 很重要,但我是 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);
});

};

最佳答案

app.use('/', routes);

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

但是您的 routes 函数期望传递一个 Express 应用程序。您应该调用以下命令,而不是上面的内容:

routes(app)

允许routes函数在express应用程序上根据需要设置路线。

关于javascript - 对象 #<IncomingMessage> 没有方法 'post',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28036726/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com