栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否有将Express应用程序转换为流星的简单方法?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否有将Express应用程序转换为流星的简单方法?[关闭]

绝对不是自动的,但是您可以将许多技巧结合在一起以 几乎 自动获得它。

我已经经历过了,这是我所有的窍门。

让我们从您的Express应用程序主.js文件开始。您需要在顶部添加以下内容:

/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

/server/main.js

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

那真的是主要的 把戏 。从这里开始它的辛勤工作。

在良好的流星风格中,我们会将所有路由渲染调用都包装到光纤中,以使其像流星服务器上的其他所有对象一样同步。为此,我们定义了一个包装函数

waiter
,可以反复使用它来包装路由函数。虽然我们添加它,我们将按摩连接请求和响应,我们将得到流星服务器上的铁干道上
res
req
对象表达想看到的。请注意:这还算是完整的。只是我想从这些对象中使​​用的签名。

/server/main.js

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(preorhtml, html) { if (html) {     // two arguments provided, treat as described     res.statusCode = preorhtml; } else {     // no pre, just html     html = preorhtml; } 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);};

最后,真正的交易:为每个指定的特快航线创建航线。为此,我们将使用铁路由器。以下代码将遍历每个定义的路径(由我们的重新定义

app
函数捕获并存储在中
routes
),并使用our将其包装在光纤中
waiter
,这还将照顾在
this.request
/
this.response
和Express应用程序假定的
req
res
对象之间进行转换。

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

这些是我完成您要问的工作所要做的最重要的事情。我敢肯定我在这里错过了一些细节,但这应该可以给你一个想法。


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

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

meteor add cmather:handlebars-server


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号