简体   繁体   中英

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. 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. All routes pass ejs views to the client with res.render() . It has routes /login , /signup , and /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.
  • App #2, also node, serves a backbone app that, as a mock, grabs a random user from the db and works for that user. It uses app.use(express.static(__dirname + "/../public")) to serve the backbone, and some routes that send json to the backbone client. It works fine, but only with a random user 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. But I want to do it without ripping out my lovely /login and /signup routes that serve server-side rendered views.

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? Can I bundle the public directory with a user id and send that bundle only on /home:uid requests?

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.
  2. res.sendFile() . This is a standard way to respond with static files. The backbone app files are static except for the user 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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