【问题标题】:How to properly organize routes in ExpressJS?如何在 ExpressJS 中正确组织路由?
【发布时间】:2014-01-19 06:22:42
【问题描述】:

我正在NodeJS 上开发我的第一个测试应用程序并遇到以下问题:我无法弄清楚如何在ExpressJS 框架中正确组织路由。例如我想做注册,所以我创建了这样的路线:

app.get('/registration', function (request, response) {
    if (request.body.user.email && request.body.user.password) {
        var user = new User();
        var result = user.createNew(request.body.user.email, request.body.user.email);

        // do stuff...
    }

    response.render('registration.html', config);
});

用户函数看起来像这样(不是最终的):

function User() {
    var userSchema = new mongoose.Schema({ 
        'email': { 
            'type': String, 
            'required': true, 
            'lowercase': true, 
            'index': { 'unique': true }
        },
        'password': {
            'type': String, 
            'required': true
        }
    });

    var userModel = mongoose.model('users', userSchema);

    this.createNew = function(email, password) {
        var new_user = new users({'email': email, 'password': password});

        new_user.save(function(err){
            console.log('Save function');

            if (err)
                return false;

            return true;
        });
    }
}

我尝试做一些结构化的应用程序,比如 MVC。问题是save 方法是异步的,每次我注册新用户时都会得到registration.html 而无需等待结果。

基本上我需要在save回调中运行路由回调,但是如何以正确的方式执行此操作我自己无法弄清楚...

【问题讨论】:

    标签: node.js express routes mongoose


    【解决方案1】:
    this.createNew = function(email, password, callback) {
        var new_user = new users({'email': email, 'password': password});
    
        new_user.save(function(err){
            console.log('Save function');
    
            if (err)
                // return false;
                callback (false);
            else
                //return true;
                callback (true);
        });
    }
    

    我发现每当我使用某个模块(例如 db)并且它使用回调时,我通常还必须对我环绕它的任何函数使用回调(除非我不关心结果)。

    这里:

    app.get('/registration', function (request, response) {
        if (request.body.user.email && request.body.user.password) {
            var user = new User();
            // var result = user.createNew(request.body.user.email, request.body.user.email);
    
            user.createNew(request.body.user.email, request.body.user.email, function (results) {
                 // do something with results (will be true or false)
                 // not sure why you have this with the actual registration stuff,
                 // but it's your site. :)
                 response.render('registration.html', config);
            });
        }
    });
    

    另外,您可能希望将对象方法放在原型中,而不是:

    this.createNew = function (....) {}
    

    试试:

    User.prototype.createNew = function ( ... ) { }
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures#Performance_considerations 了解原因。

    【讨论】:

      【解决方案2】:

      如需组织 Node.js 应用程序的起点,请尝试通读此演示 express.js 应用程序的源代码。 Click here for the link

      这是一个相当流行的存储库,我发现自己在从头开始构建 Node.js 应用程序时经常引用它。

      它可能是您的项目的一个很好的参考,因为它是以 MVC 风格完成的并且它使用猫鼬。这些路线被组织成一个文件,可以在config/routes.js 中找到。您还应该查看 app/models/ 中的模型,以了解组织用户模型的替代方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-27
        • 2011-06-03
        • 2017-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多