有没有一种简单的方法可以将快递应用程序转换为流星? [英] Is there an easy way to convert an express app to meteor?

查看:32
本文介绍了有没有一种简单的方法可以将快递应用程序转换为流星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有 express 框架应用程序的 node.js 转换为meteor.基本上做 https://github.com/onmodulus/demeteorizer

I am trying to convert a node.js with express framework app to meteor. Essentially doing the reverse way of https://github.com/onmodulus/demeteorizer

推荐答案

绝对不是自动的,但是有很多技巧可以链接在一起以几乎自动获得它.

Definitely not automatically, but there are a bunch of tricks you can chain together to almost get it automatically.

我已经经历过了,这里是我的所有技巧.

I've been through just that and here are all of my tricks for this.

让我们从您的 express 应用主 .js 文件开始.这个你需要在顶部添加以下内容:

Let's start with your express app main .js file. This one you need to add the following at the top:

/server/main.js:

routes = {};
var app = { 
    get: function(route, foo) {
        // routes.get[route] = foo;
        routes[route] = foo;
    }, 
    all: function(route, foo) {
        // routes.all[route] = foo;
        routes[route] = foo;
    } 
};

所有这些都是定义你需要的app函数,并将定义的路由记录在一个对象中,我们稍后将使用iron-router.因此,这可以确保在 routes 中记录如下内容:

All this does is to define the app functions you need, and record the defined routes in an object, which we will later use to define those routes using iron-router. So this makes sure that things like the following get recorded in routes:

/server/main.js:

app.get('/show', function(req, res) {
    res.render('mytemplate');
});

这确实是主要的技巧.从这里开始它只是劳动.

That's really the main trick. From here on its just labor.

在良好的流星风格中,我们将所有路由渲染调用封装到一个纤程中,使它们像流星服务器上的其他所有内容一样同步.为此,我们定义了一个包装函数 waiter,我们可以反复重用它来包装路由函数.当我们添加它时,我们将从流星服务器上的铁路由获得的连接请求和响应传递到 resreq 对象中希望看到.请注意:这绝不是完整的.这只是我想从这些对象中使​​用的签名.

In good meteor style, we will wrap all route rendering calls into a fiber, to make them synchronous like everything else on the meteor server. For that, we define a wrapping function waiter that we can reuse over and over again to wrap the route functions. And while we are add it, we will massage the connect request and response that we will get from the iron-routes on the meteor server into the res and req objects express would like to see. Mind you: this is not complete by any stretch. It's just the signatures I wanted to use from these objects.

/server/main.js:

/** create an sync version for meteor */
waiter = function(foo, req, res) {
    var waiter_aux = Meteor._wrapAsync(function(foo, req, res, callback) {

        res.set = function(header, value) {
            res.setHeader(header, value);
        };

        res.send = function(codeorhtml, html) {
            if (html) {
                // two arguments provided, treat as described
                res.statusCode = codeorhtml;
            } else {
                // no code, just html
                html = codeorhtml;
            }
            callback(null, html);
        };

        res.render = function(name, data, callback) {
            callback = callback || function(err, html) {
                res.send(html);
            };

            var html = Handlebars.templates[name](data);
            callback(null, html);
        };

        res.json = function(object) {
            res.send(JSON.stringify(object));
        }

        res.redirect = function(URL) {
            res.writeHead(302, {
                'Location': URL
            });
            res.end();
        };

        req.header = function(x) {
            return this.header[x];
        };

        TemplatesObject = Handlebars.templates;

        // these objects need to be extended further
        foo(req, res);
    });

    return waiter_aux(foo, req, res);
};

最后,真正的交易:为每个指定的快速路线创建路线.为此,我们将使用 iron-router.以下代码将遍历每个定义的路由(由我们重新定义的 app 函数捕获并存储在 routes 中),并使用我们的 waiter,它还负责在 this.request/this.responsereqres 之间进行翻译> 对象表达应用假设.

Finally, the real deal: creating routes for each specified express route. For this we will use iron-router. The following code will go through each defined route (caught by our redefined app functions and stored in routes), and wrap it in a fiber using our waiter, which will also take care of translating between this.request/this.response and the req and res objects express apps assume.

/routes.js:

if (Meteor.isServer) {
    // create routes for all the app.get's and app.all's in bibbase.js
    // (server)
    console.log("setting routes:", routes);
    _.each(routes, function(foo, route) {
        Router.map(function () {
            this.route(route, {
                path: route,
                where: 'server',
                action: function() {
                    this.request.params = this.params;
                    var html = waiter(foo, this.request, this.response);
                    if (!this.response.statusCode) {
                        this.response.statusCode = 200;
                    }
                    if (!this.response.getHeader('Content-Type')) {
                        this.response
                            .setHeader('Content-Type', 'text/html');
                    }
                    this.response.end(html);
                }
            });
        });
    });

}

<小时>

这些是我为完成您的要求所做的最重要的事情.我确定我在这里遗漏了一些细节,但这应该会给你一个想法.


These are the most essential things I've done to accomplish what you are asking about. I'm sure I've missed a few details here, but this should give you an idea.

空格键后的更新(我忘记了那个版本的 Meteor):

Update for post-Spacebars (I forget which version of Meteor that was):

为了完成这项工作,您现在需要添加handlebars-server:

In order to make this work, you now need to add handlebars-server:

meteor add cmather:handlebars-server

这篇关于有没有一种简单的方法可以将快递应用程序转换为流星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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