mercredi 1 avril 2015

In node with express, how to render server-side views for some routes, backbone for others?

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 (e.g. 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.


Aucun commentaire:

Enregistrer un commentaire