Node.js + Connect(Node.js + Connect)

编程入门 行业动态 更新时间:2024-10-23 06:20:48
Node.js + Connect(Node.js + Connect)

我需要一些解释。 我有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(); }

更多推荐

本文发布于:2023-08-01 02:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Node   js   Connect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!