已经进行到第一章注册和登录那部分
修改完style.css文件后运行app.js提示
Error: .get() requires callback functions but got a [object Undefined]
at D:\Blog\node_modules\express\lib\router\index.js:252:11
at Array.forEach (native)
at Router.route (D:\Blog\node_modules\express\lib\router\index.js:248:13)
at Router.(anonymous function) [as get] (D:\Blog\node_modules\express\lib\ro
uter\index.js:270:16)
at Function.app.(anonymous function) [as get] (D:\Blog\node_modules\express
lib\application.js:414:26)
at Object.<anonymous> (D:\Blog\app.js:41:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions…js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
请问是哪出错了?
module.exports = function(app){ app.get('/’,function(req,res){ res.render('index’, { title: ‘主页’ }); }); app.get('/reg’,function(req,res){ res.render('reg’, { title: ‘注册’ }); }); app.post('/reg’,function(req,res){ }); app.get('/login’,function(req,res){ res.render('login’, { title: ‘登录’ }); }); app.post('/login’,function(req,res){ }); app.get('/post’,function(req,res){ res.render('post’, { title: ‘发表’ }); }); app.post('/post’,function(req,res){ }); app.get('/logout’,function(req,res){ }); }; 完全按教程。。
/**
- Module dependencies. */
var express = require(‘express’) , routes = require(‘./routes’) , user = require(‘./routes/user’) , http = require(‘http’) , path = require(‘path’) , MongoStore = require(‘connect-mongo’)(express) , settings = require(‘./settings’);
var app = express();
// all environments app.set('port’, process.env.PORT || 3000); app.set('views’, __dirname + ‘/views’); app.set('view engine’, ‘ejs’); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: settings.cookieSecret, key: settings.db, cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days store: new MongoStore({ db: settings.db }) })); app.use(app.router); app.use(express.static(path.join(__dirname, ‘public’)));
// development only if (‘development’ == app.get(‘env’)) { app.use(express.errorHandler()); }
app.get('/’, routes.index); app.get('/users’, user.list);
http.createServer(app).listen(app.get(‘port’), function(){ console.log('Express server listening on port ' + app.get(‘port’)); });* 这也完全照教程来的 41行就是app.get('/’, routes.index); 没啥不对吧
贴代码还是按markdown 格式来贴容易看
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
@741144175 routes/index.js exports.index = function(req, res){ res.render('index’, { title: ‘Express’ }); }; exports.login = function(req,res){ res.render('login’,{title:’用户登录’}); }; 这样写, app.js 这样写 app.get('/’, routes.index); app.get('/login’,routes.login); app.post('/login’,routes.doLogin);
@xianggp 仍然错误 求帮助 index.js exports.index = function(req, res){ res.render('index’, { title: ‘主页’ }); }; exports.login = function(req,res){ res.render('login’,{title:’登录’}); }; exports.reg = function(req,res){ res.render('reg’,{title:’注册’}); }; exports.post = function(req,res){ res.render('post’,{title:’发表’}); }; exports.logout = function(req,res){ res.render('logout’,{title:’登出’}); };
app.js var express = require(‘express’) , routes = require(‘./routes’) , user = require(‘./routes/user’) , http = require(‘http’) , path = require(‘path’) , MongoStore = require(‘connect-mongo’)(express) , settings = require(‘./settings’);
var app = express();
// all environments app.set('port’, process.env.PORT || 3000); app.set('views’, __dirname + ‘/views’); app.set('view engine’, ‘ejs’); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: settings.cookieSecret, key: settings.db, cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days store: new MongoStore({ db: settings.db }) })); app.use(app.router); app.use(express.static(path.join(__dirname, ‘public’)));
// development only if (‘development’ == app.get(‘env’)) { app.use(express.errorHandler()); }
app.get('/’, routes.index); app.get('/login’,routes.login); app.post('/login’,routes.doLogin);
http.createServer(app).listen(app.get(‘port’), function(){ console.log('Express server listening on port ' + app.get(‘port’)); });
错误提示 D:\Blog\node_modules\express\lib\router\index.js:252 throw new Error(msg); ^ Error: .post() requires callback functions but got a [object Undefined] at D:\Blog\node_modules\express\lib\router\index.js:252:11 at Array.forEach (native) at Router.route (D:\Blog\node_modules\express\lib\router\index.js:248:13) at Router.(anonymous function) [as post] (D:\Blog\node_modules\express\lib\r outer\index.js:270:16) at Function.app.(anonymous function) [as post] (D:\Blog\node_modules\express \lib\application.js:414:26) at Object.<anonymous> (D:\Blog\app.js:43:5) at Module._compile (module.js:456:26) at Object.Module._extensions…js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12)
index.js exports.index = function(req, res){ res.render('index’, { title: ‘主页’ }); }; exports.login = function(req,res){ res.render('login’,{title:’登录’}); }; exports.reg = function(req,res){ res.render('reg’,{title:’注册’}); }; exports.post = function(req,res){ res.render('post’,{title:’发表’}); }; exports.logout = function(req,res){ res.render('logout’,{title:’登出’}); };
@741144175 app.js 只有这三个吗? '’’app.get('/’, routes.index); app.get('/login’,routes.login); app.post(‘/login’,routes.doLogin);’’’
那你路由里面有 '’’exports.reg = function(req,res){ res.render('reg’,{title:’注册’}); }; exports.post = function(req,res){ res.render('post’,{title:’发表’}); }; exports.logout = function(req,res){ res.render(‘logout’,{title:’登出’}); };’’’ 这三个没有配置出来 app.js 添加 '’’app.get('/reg’, routes.reg); app.get('/post’,routes.post); app.post(‘/logout’,routes.logout);’’’
@xianggp 依旧报错 是不是Index里的post部分要咋样?
app.get('/', routes.index);
app.get('/login',routes.login);
app.post('/login',routes.doLogin);
app.get('/reg', routes.reg);
app.get('/post',routes.post);
app.post('/logout',routes.logout);
@junyuecao 麻烦看看= =说post没有callback
D:\Blog\node_modules\express\lib\router\index.js:252
throw new Error(msg);
^
Error: .post() requires callback functions but got a [object Undefined]
at D:\Blog\node_modules\express\lib\router\index.js:252:11
at Array.forEach (native)
at Router.route (D:\Blog\node_modules\express\lib\router\index.js:248:13)
at Router.(anonymous function) [as post] (D:\Blog\node_modules\express\lib\r
outer\index.js:270:16)
at Function.app.(anonymous function) [as post] (D:\Blog\node_modules\express
\lib\application.js:414:26)
at Object.<anonymous> (D:\Blog\app.js:43:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)