Javascript Node.js+;连接
我需要一些解释。 我有app.js: …使用(重写) .使用(…) rewrite.js:Javascript Node.js+;连接,javascript,node.js,Javascript,Node.js,我需要一些解释。 我有app.js: …使用(重写) .使用(…) rewrite.js: .... module.exports = function(req, res, next) { ..... if (match) { findPostIdBySlug(match[1], function(err, id) { ..... next(); }); .... 我没有创建findPostIdBySlug()函数 我的意思是当我尝试时: var findPostIdB
....
module.exports = function(req, res, next) {
.....
if (match) {
findPostIdBySlug(match[1], function(err, id) {
.....
next();
});
....
我没有创建findPostIdBySlug()函数
我的意思是当我尝试时:
var findPostIdBySlug = function() {
return;}
没什么事。程序在rewrite.js中的next()之前停止。我必须在代码中实现这个函数(findPostIdBySlug),才能在没有挂起的情况下运行?如果next()
在匿名函数的主体中,那么我应该将函数本身放置在哪里
findPostIdBySlug(match[1], function(err, id) {
next();
});
然后,findPostIdBySlug()
至少需要调用该函数
:
var findPostIdBySlug = function (slug, callback) {
callback();
};
这样它就可以依次调用next()
还要注意,中间件中的所有路径都应该指向next()
或响应。包括不存在匹配项时
和不调用findPostIdBySlug
的情况
if (match) {
findPostIdBySlug(match[1], function(err, id) {
next();
});
} else {
next();
}