nodejs表示中间件函数的返回值 [英] nodejs express middleware function return value

查看:239
本文介绍了nodejs表示中间件函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NodeJS和Express,我的路由如下,中间件功能是Mobile.如果我不使用return next();在isMobile函数中,该应用程序卡住了,因为NodeJS不会移至下一个函数.

I'm using NodeJS and Express, I have the following route and the middleware function isMobile. If I don't use return next(); in the isMobile function, the app gets stuck because NodeJS does not move over to the next function.

但是我需要isMobile函数来返回一个值,这样我才能在app.get中进行相应的处理.有什么想法吗?

But I need the isMobile function to return a value so I can process accordingly in the app.get. Any ideas?

app.get('/', isMobile, function(req, res){
 // do something based on isMobile return value
});



function isMobile(req, res, next) {

    var MobileDetect = require('mobile-detect');
    md = new MobileDetect(req.headers['user-agent']);

    //return md.phone(); // need this value back

    return next();
}

谢谢.

推荐答案

您有两种选择:

  1. 将值附加到req对象:

app.get('/', isMobile, function(req, res){
   // Now in here req.phone is md.phone. You can use it:
   req.phone.makePrankCall();
});

function isMobile(req, res, next) {
    var MobileDetect = require('mobile-detect');
    md = new MobileDetect(req.headers['user-agent']);

    req.phone = md.phone();
    next();// No need to return anything.
}

这是多少个Express/Connect中间件传递值.就像将主体属性附加到请求对象的bodyParser或将会话属性附加到请求对象的会话中间件一样.

This is how many of express/connect middlewares pass values. Like bodyParser which attaches body property to request object, or session middleware which attaches session property to request object.

尽管请注意,您必须注意其他任何库都不会使用该属性,所以不会发生冲突.

Though note that you have to be careful that no other library uses that property, so there's no conflicts.

  1. 不要使其成为中间件,只需直接使用该功能即可.如果它不是像上面那样异步的,并且不会在所有路由中全局使用(不会有这样的东西:app.use(isMobile)),那么这也是一个很好的解决方案:

  1. Don't make it a middleware, just use the function directly. If it's not asynchronous like above, and not gonna be used globally for all routes(not gonna have something like this: app.use(isMobile)), this also a good solution:

app.get('/', function(req, res){
   var phone = isMobile(req);
   phone.makePrankCall();
});

function isMobile(req) {
    var MobileDetect = require('mobile-detect');
    md = new MobileDetect(req.headers['user-agent']);

    return md.phone();
}

如果计算成本很高,并且您可能在多个中间件中使用它,则可以使用WeakMap(

If it's expensive to compute, and you might use it in more than one middleware you can cache it with a WeakMap(or use a module that does it for you):

    app.get('/', function(req, res){
       var phone = isMobile(req);
       phone.makePrankCall();
    });

    var cache = new WeakMap()

    function isMobile(req) {
        var MobileDetect = require('mobile-detect');

        result = cache.get(req);

        if (result) {
            return result;
        } else {
            md = new MobileDetect(req.headers['user-agent']).phone();
            cache.set(req, md);
            return md;
        }
    }

这篇关于nodejs表示中间件函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆