我运行localhost:3000 / name / john,创建一个cookie 然后localhost:3000 / name显示cookie 我有一个控制台错误“TypeError:无法读取属性'名称'未定义”
查看我的代码
var express = require('express')
, http = require('http')
, path = require('path') ;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser()) ;
app.use(express.methodOverride()) ;
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret'}));
}) ;
app.get('/name/:name', function(req, res){
res.cookie('name' , req.params.name).send('<p>the cookie <a href="/name">Go here</a></p>') ;
});
app.get('/name', function(req, res){
console.log(req.cookies.name);
res.send("index");
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
如何解决这个问题?感谢
答案 0 :(得分:1)
修改您的app.configure代码,如下所示。
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser()) ;
app.use(express.methodOverride()) ;
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret'}));
app.use(app.router);
}) ;
看看http://expressjs.com/api.html#app.use。这里的问题在于中间件优先级。