Can I use a Node.js ‘mirror’ as fallback when JavaScript not supported?

The basic question

Is it possible to fallback to a server-side Node.js
implementation of an existing client-side, JavaScript-heavy site. Specifically for users who don’t have JavaScript (or perhaps, if the client’s browser fails to meet certain performance requirements)?

I’m really after an architecture thats a literal interpretation of slide 8 of this presentation
.

Rather than mustache/backbone forming data structures and creating views on the clientside, instead this would be done in Node and the the fully-formed HTML could be returned.

Background

I have written an application that is built on a stack of jQuery, Underscore, Backbone and Mustache (making RESTful API calls to a web service for data).

This all works very well with JavaScript enabled in the browser, but I haven’t yet written a server-side implementation as a fallback.

Of course I could essentially ‘re-write everything’ in C#.NET/Python/Ruby/etc., but this seems like a very poor solution, when you consider the amount of duplication and added maintenance, when there is a fully functional version of the application available in JavaScript.

Problem courtesy of: isNaN1247

Solution

It looks as though this is probably something that we’ll see more an more of as we go forward in the world of server-side JS.

I might have a play with writing something myself, however as commenter (on the OP) juandopazo
mentions, this is possible already with YUI3.

Douglas Crockford mentions exactly what I’m on about (and YUI’s cabailities) in this video on ‘Serversideness’
– see from 03:45.

Solution courtesy of: isNaN1247

Discussion

You can do this. It is even easier using something like express.js: http://expressjs.com/

Express.js, for example, has a function called res.render() which will render a template for you (in a wide array of templating languages).

The functionality of your client side app will have to be rewritten to work without js (clearly). But you could even dynamically generate anchor tags and pass them in as variables to your templates on the server.

Example responding to whatever.com/test that renders the template index.jade with the title variable set to ‘My Site’:

app.get('/test', function(req, res){
    res.render('index.jade', { title: 'My Site' });
});

As for reusing your entire backbone app on the server, I would imagine it is possible using jsdom and rewriting a ton of code. It most likely more cost-effective to just rewrite your app in a different style using requests and templates.

Discussion courtesy of: Chris Biscardi

This recipe can be found in it’s original form on Stack Over Flow
.

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Can I use a Node.js ‘mirror’ as fallback when JavaScript not supported?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录