有没有一个简单的方法来转换明星应用程序流星? [英] Is there an easy way to convert an express app to meteor?

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

问题描述

我正在尝试将一个具有express framework应用程序的node.js转换为流星。
基本上做相反的方式 https://github.com/onmodulus/demeteorizer

解决方案

绝对不是自动的,但有一些技巧可以链接到几乎自动获取



我已经完成了,这里是我所有的技巧。



让我们开始吧与您的快递应用程序主要.js文件。这个你需要在顶部添加以下内容:



/server/main.js

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

所有这些都是定义应用程序您需要的功能,并将定义的路线记录在对象中,我们稍后将使用它们来定义这些路由,使用 iron-router 。所以这样可以确保以下内容记录在路线中



/ server /main.js

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

这真的是主要的技巧。



在流星的流行风格中,我们将把所有路线渲染调用包装到光纤中,使其与流星服务器上的其他任何内容同步。为此,我们定义一个包装函数 waiter ,我们可以一遍又一遍地重用来包装路由功能。当我们添加它时,我们将按照我们从流星服务器上的铁路路线获取的连接请求和响应,进入 res req 对象express想要看到。记住你:这是不完整的任何伸展。这只是我想从这些对象使用的签名。



/server/main.js

  / **为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){
//提供了两个参数, $ b res.statusCode = codeorhtml;
} else {
//没有代码,只是html
html = codeorhtml;
}
回调(null,html);
};

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

var html = Handlebars.templates [name](data);
回调(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;

//这些对象需要进一步扩展
foo(req,res);
});

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

最后,真正的交易:为每个指定的快速路线创建路由。为此,我们将使用铁路由​​器。以下代码将通过每个定义的路由(由我们重新定义的应用程序函数捕获并存储在路由中),并包装它在纤维中使用我们的服务员,这也将照顾在 this.request之间的翻译 / this.response req res 对象表示应用程序假设。 p>

/routes.js

 code> if(Meteor.isServer){
//为bibbase.js中的所有app.get和app.all创建路由
//(server)
console.log (设定路线:,路线);
_.each(routes,function(foo,route){
Router.map(function(){
this.route(route,{
path:route,
其中:'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);
}
});
});
});

}






是完成你所要求的最重要的事情。我相信我在这里错过了一些细节,但这应该给你一个想法。






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



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

 流星添加cmather:handlebars-server 


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.

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;
    } 
};

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.

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);
};

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.


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

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

meteor add cmather:handlebars-server

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

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