简体   繁体   English

如何在node / express和Backbone中混合混合(服务器端和客户端渲染)?

[英]How to mix hybrid (server-side and client-side rendering) in node/express and Backbone?

I'm new to node, and to stack overflow, so only answer if you're patient. 我是node的新手,并且是堆栈溢出的对象,所以只有耐心地回答。 I'm having a surprisingly hard time integrating code that currently exists in two different little node apps. 我在集成两个不同的小节点应用程序中当前存在的代码时遇到了令人惊讶的困难。 I have a hard time believing I'm the first person with this problem, but I haven't found a similar question or example ( eg This similar question is only theoretical) 我很难相信我是这个问题的第一人,但是我没有找到类似的问题或例子( 例如,这个类似的问题只是理论上的)

What I have: 我有的:

  • App #1 implements a working, basic login wall that uses node, express, passportjs. App#1实现了一个工作正常的基本登录墙,该登录墙使用了node,express,passportjsjs。 All routes pass ejs views to the client with res.render() . 所有路由都使用res.render()将ejs视图传递给客户端。 It has routes /login , /signup , and /home . 它具有/login/signup/home路由。 It works great, and when a user is logged in or signed up, the /home route serves them an ejs, node-rendered home page displaying their info. 它运作良好,当用户登录或注册时, /home路由为他们提供一个ejs(节点呈现的主页),显示其信息。
  • App #2, also node, serves a backbone app that, as a mock, grabs a random user from the db and works for that user. 应用程序#2(也是节点)提供了一个主干应用程序,该应用程序作为模拟对象,从数据库中获取了一个随机用户并为该用户工作。 It uses app.use(express.static(__dirname + "/../public")) to serve the backbone, and some routes that send json to the backbone client. 它使用app.use(express.static(__dirname + "/../public"))服务于骨干网,以及一些将json发送到骨干网客户端的路由。 It works fine, but only with a random user id. 它可以正常工作,但只能使用随机用户ID。

What I want: In App #1, I want a route, like 'home:uid' to render the backbone app for the user with that uid. 我想要的是:在应用程序1中,我想要一条路由,例如'home:uid'以使用该uid为用户呈现主干应用程序。 But I want to do it without ripping out my lovely /login and /signup routes that serve server-side rendered views. 但我想做到这一点而又不破坏我提供服务器端渲染视图的可爱的/login/signup路由。

Finally my question: How can I have the flow the node routes switch, after login, from serving server-side rendered views to backbone? 最后是我的问题:登录后,节点路由如何从服务端渲染视图切换到骨干网? How do I make the user id (in the req object) available to the backbone app that I'm sending to the client? 如何使用户ID(在req对象中)可用于我发送给客户端的骨干应用程序? Can I bundle the public directory with a user id and send that bundle only on /home:uid requests? 我可以将公共目录与用户ID捆绑在一起,并且仅在/home:uid请求上发送该捆绑包吗?

Things I've tried and could use more direction on: 我已经尝试过并且可以在以下方面使用更多指导的事情:

  1. I can use app.use(express.static(__dirname + "/../public"), {index: false}) seems useful, but I'd like an example of how to user express static with index set to false. 我可以使用app.use(express.static(__dirname + "/../public"), {index: false})似乎有用,但是我想举一个如何在索引设置为false的情况下表达静态的示例。
  2. res.sendFile() . res.sendFile() This is a standard way to respond with static files. 这是响应静态文件的标准方法。 The backbone app files are static except for the user id. 除用户ID外,主干应用程序文件都是静态的。 I'm not sure how to handle that. 我不确定如何处理。

Thanks for patience & help. 感谢您的耐心配合和帮助。

This post by Airbnb seems to say that mixing client-side and server-side rendering in node is a frontier; Airbnb的这篇文章似乎说,在节点中混合客户端和服务器端渲染是一个前沿。 node isn't set up to, in one app, switch from serving files templated on the backend to serving static files that render on the front-end end. 节点未设置为在一个应用程序中从在后端模板化的服务文件转换为在前端呈现的静态文件的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM