Node.js와 Express 3.0을 사용하고 있으며 app.locals와 res.locals의 차이점에 대해 매우 명확합니다 ...Node.js 및 Express 3 : 비동기 적으로로드되는 app.locals 설정
내 EJS 템플릿에서 전역 적으로 참조 할 app.locals 변수 ('latestest'및 'popular')를 설정하고 싶습니다. 이러한 변수에 대한 데이터는 데이터베이스 (CouchDB/Cradle)에서 가져 오며 데이터베이스에서 내 응용 프로그램 데이터를 노출하기 위해 단순히 res.json으로 수행하는 my app.routes (즉 :/myappdata) 중 하나를 통해 액세스 할 수도 있습니다.
그래서이 물건을 넣기위한 가장 좋은 방법은 내 app.locals에 한번? 라우터 앞에 app.use()가있는 미들웨어 함수를 만들어야합니다. 내 요청이있을 때마다 그리고 앱이 시작될 때만 데이터베이스가 호출되지 않도록하려면 어떻게해야합니까? app.use()에서 비동기 콜백은 어떻게해야합니까?
또한 직접 app.locals() 설정을 시도했지만 '최신'및 '인기있는'변수는 특정 시간에 내 템플릿에 "정의되지 않은"것으로 보입니다. 여기
(어쩌면 뭔가 app.locals ??에 들어서는 것은) 내 'app.js는'시작시 server.js에 반환됩니다 : 어떤 제안에 대한
var express = require('express'),
engine = require('ejs-locals'),
conf = require('./conf'),
cradle = require('cradle').(conf.databaseConn),
app = express();
exports.init = function(port) {
app.locals({
_layoutFile:'layout.ejs',
newest:{}, // like to set this from db once
popular:{} // like to set this from db once
});
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.compress());
app.use(express.static(__dirname + '/static'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.cookieSession({cookie:{path:'/',httpOnly:true,maxAge:null},secret:'blh'}));
app.use(function(req, res, next){
res.locals.path = req.path;
res.locals.user = req.session.user;
next();
});
app.use(app.router);
});
app.engine('ejs', engine);
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
})
app.use(function(err, req, res, next){
res.render('500.ejs', { locals: { error: err, path:"" },status: 500 });
});
var server = app.listen(port);
console.log("Listening on port %d in %s mode", server.address().port, app.settings.env);
return app;
}
감사합니다.
미안하지만 나는 미들웨어와 같은 줄을 생각하기 시작했다. 나는 보통처럼 콜백을 처리 할 수있을 것이라고 생각한다. 반환 할 때까지 next()를 호출하지 않을 것이다. – ZimSystem