已经进行到第一章注册和登录那部分
修改完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)
请问是哪出错了?
index.js 也贴出来看下
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){ }); }; 完全按教程。。
D:\Blog\app.js:41:5
看下
/**
- 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); 没啥不对吧
@741144175 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函数
@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)
@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:‘登出’}); };
@booxood 麻烦看一下 谢谢
@741144175 代码第一行的前一行和最后一行的后一行写上```,中间的就变成代码了
@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 ‘’’ 都去掉
@741144175 好囧,两点.1:打的是数字1左边的那个键```,不是英文标点’’'2: 要独立一行写那三个符号
@junyuecao ``` 谢谢
@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)
要注意你的routes/index.js的写法 如果是这样的话
exports.index = function(req,res){
//todo
}
那app.js里就要这样
app.get('/',routes.index);
如果routes/index.js是这样的话
module.exports = function(app){
app.get('/',function(req,res){
//todo
});
}
那app.js里是这样写的
routes(app);
@junyuecao 谢谢!ok了 那请问在使用css框架方面有啥要注意的么
@741144175 给自己做的东西的话,样式问题就看自己喜好啦.
@741144175,能把您修改后对的代码贴出来吗,我也遇到了同样的问题。也照着上面的方案改了下,还是不行啊。