我正在处理简单的登录表单,登录后(成功),我希望呈现产品列表。但是登录表单本身不会被渲染。这是我所拥有的app.js
app.get ( '/users/new', routes.api.user.isLogged, routes.api.user.new );
该isLogged
方法只告诉会话是否存在。那里似乎没有问题。
在我的user.js
文件中,在新方法中,这就是我得到的:
exports.new = function(req, res) {
res.format({
html: function() {
return res.render('users/new', {title: 'Login'});
}
});
};
有时,它会毫不犹豫地渲染,有时它会弹出一个错误说
domain.js:66
throw er;
^
Error: Can't render headers after they are sent to the client.
at ServerResponse.OutgoingMessage._renderHeaders (http.js:715:11)
at ServerResponse.res._renderHeaders (/Users/kaushik/dev/my_repos/retailer-catalog-backend/node_modules/kue/node_modules/express/node_modules/connect/lib/patch.js:76:25)
at ServerResponse.writeHead (http.js:1083:20)
at ServerResponse._implicitHeader (http.js:1040:8)
at ServerResponse.module.exports.res.write (/Users/kaushik/dev/my_repos/retailer-catalog-backend/node_modules/express/node_modules/connect/lib/middleware/compress.js:79:34)
at ondata (stream.js:38:26)
at EventEmitter.emit (events.js:96:17)
at ReadStream._emitData (fs.js:1368:10)
at afterRead (fs.js:1350:10)
at Object.wrapper [as oncomplete] (fs.js:362:17)
我真的不明白为什么会这样。我是表达和节点的新手。
编辑
isLogged
方法有:
exports.isLogged = function(req, res, next) {
if (req.session.user) {
res.redirect('/' + req.session.user.username + '/retailers')
} else{
return next();
};
};
这是app.js
服务器配置:
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.compress());
app.use(express.methodOverride());
app.use(require('less-middleware')({ src: __dirname + '/routes/web/public' }));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: '5234523451' }));
app.use(validator);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'routes', 'web', 'public')));
});