我需要一些解释。 我有app.js:
...使用(重写).use(...)
rewrite.js:
.... module.exports = function(req, res, next) { ..... if (match) { findPostIdBySlug(match[1], function(err, id) { ..... next(); }); ....我没有创建函数findPostIdBySlug()我的意思是当我尝试时:
var findPostIdBySlug = function() { return;}一切都没有。 程序只在rewrite.js中的next()之前停止。 如何在代码中实现此函数(findPostIdBySlug)以便在没有挂起的情况下运行? 我应该在哪里放置功能?
I need some explanations. i have app.js:
...use(rewrite) .use(...)
rewrite.js:
.... module.exports = function(req, res, next) { ..... if (match) { findPostIdBySlug(match[1], function(err, id) { ..... next(); }); ....I did not get to create a function findPostIdBySlug() I mean when i try just:
var findPostIdBySlug = function() { return;}nothing going on. Program just stop before next() in rewrite.js. What do I have to implement this function (findPostIdBySlug) in the code to run without hang-ups? And where should I place the function itself?
最满意答案
如果next()在匿名function体内:
findPostIdBySlug(match[1], function(err, id) { next(); });然后, findPostIdBySlug()需要至少调用该function :
var findPostIdBySlug = function (slug, callback) { callback(); };这样它就可以调用next() 。
另请注意,中间件中的所有路径都应该导致next()或响应。 包括没有match ,将不会调用findPostIdBySlug 。
if (match) { findPostIdBySlug(match[1], function(err, id) { next(); }); } else { next(); }If next() is within the anonymous function's body:
findPostIdBySlug(match[1], function(err, id) { next(); });Then, findPostIdBySlug() needs to at least call that function:
var findPostIdBySlug = function (slug, callback) { callback(); };So that it can in turn call next().
Also note that all paths in a middleware should lead to next() or a response. Including when there isn't a match and findPostIdBySlug won't be called.
if (match) { findPostIdBySlug(match[1], function(err, id) { next(); }); } else { next(); }更多推荐
发布评论