Share template between client and server - javascript

I am building an app - front end = backbone , server = node.js.
In order to improve performance, I am thinking on rendering first page from node/express server, using some templating engine. On client I want to do all iterations via ajax requests (receiving json) and, again, using template to render the data (actually it is the same template).
Is there a way to reuse the same template on client and server? It will be easier to maintain / cache. The first load of the template on the server has other advantages like server routing + option to determind which template to return according to user agent.
Thanks!!

You can use ECT template engine. ECT designed for use on client and server without any modifications in templates and code. Also ECT most fastest template engine for now.

Related

Passing NodeJS data to Javascript

I have a web application with a client that receives data from a server. I have the data in NodeJS, but I want to pass the data to a Javascript file. The Javascript file is included in a HTML file, so I can't make the files communicate with eachother.
I am new to NodeJS, so it can be a stupid question, but anyones help is appreciated
This is for a project where I need have a data stream, and I need to pass it into a web application. I tried to pass the data to different page inside my application and then I tried to get that data on that page inside my web application via Javascript, but I couldn't make that work. I'm not even sure if its possible at this point.
Your node server can't communicate with your front-end without a specific way of communication like websocket, you have many other way to communicate with your front-end as node-server, take a look at server send event for example.
By the way your front-end can call your node server more easely with a get request as said #tomerpacific in comment.
For that you have to open a route with your express app. Routing with express
And for call it on a GET request, for that you can use the XMLHttpRequest, and if you have implemented jQuery on your front, you can use Ajax jQuery.

REST Client: java backend consumer vs java script consumer

I want to generate webApp in java and just use some REST service. I think there are two approach for this:
1- Client side consumer: With an simple app that contain some java script file which call service and then generate UI from result.
2- Server side consumer: In backend first call service(with spring RestTemplate) then generate appropriate UI and send HTML ui to client.
Which approach is recommended?
I know this question is very general but I want to know advantage and disadvantage of those.
Client side consumer approach is the better one as tomorrow if you have one more client application who requires the same data but will be displaying it in a different way then client side consumer approach will benefit you. For eg: suppose the consumer today is a web page then you can get the data from backend and display it on your webpage, but tomorrow if your business grows and you plan for a mobile app then in that case your same rest api will come in handy for you.

In sails js should we plug a mongoDB database calculating app into a POST Controller?

The main interface app will return variables based on those initially POSTED by the client, and subsequent database calculations performed in real time by a dedicated engine.
In Sails can we plug the engine into a Controller used for returning the calculated variable?
What would be the best way to implement a real time link between the client and the engine ?
Sails comes with sockets support built in. You can transmit the data out of your controller back to the client via sockets to keep everything in sync.
Reference this page for sockets in sails:
https://gist.github.com/mikermcneil/6598661
As an aside, you could do everything using sockets, including the posting.
What is this 'dedicated engine'? Is this a separate service running somewhere else, or is it just logic for processing this data and handing it back to the controller?
If you want to put the data processing logic in the same app you can create a service which exports whatever data processing functions you need. Then in your controller that is handling the POST requests you can call on those services as needed, process the data, and emit it back to the client. All your sockets logic can go in that same controller since it is for communicating with the client interface. I would consider just moving all of it to sockets. If you look at the sails docs you will see that it has a similar interface with sockets where you can do standard CRUD operations: sockets.PUT, etc.
Sails.js WebSockets
No framework other than Sails, unless necessary for security purpose
Minimalist architecture implemented in Sails
1 POST API
1 POST Model
1 POST Controller
1 Socket per processing method
1 Socket switch within the Controller
Now if a secured framework integrates socket switching within controllers, that would come useful.
We may add middleware to the POST Model for the purpose of filtering text input data...

Format in markdown server or client side? nodejs

I want to use markdown in a website, I'm wondering about convert markdown to HTML client side or server side. I'll use https://github.com/evilstreak/markdown-js that has both client/server libraries. (node.js)
I never used markdown before and I'm wondering about the efficience of the two ways, the security too, because I don't want to have html tags from users (injections)
Do you have any advice or explanation about why do it client side or server side? Thanks.
If you're looking for efficiency, you should use the same module to render at both server-side and client-side.
Server side rendering is needed for initial request, and if your data is updated somehow by user, it needs to be re-rendered at the client-side.
Reasons:
client-side rendering is too slow for initial request because user needs to fetch all libraries first
server-side rendering is too slow for using after the page is loaded because it needs additional requests

Express + Jade rendering - client or server side?

I just started using ExpressJS and Jade and I was sure that everything is server-side but this post makes me a little bit confused(because my site behaves like Client-side scenario): https://stackoverflow.com/a/12291675
I guess that node.js only sends whole site once and then sends JSON data, because the rest is loaded in browser cache?
So it would be helpful if someone describe this mechanism to me.
You can code an express.js app to render jade templates into HTML on the server side and send HTML to the browser. This is the more traditional approach. However, jade is also capable of running in the browser, so your express app can send jade templates (either as jade syntax text or pre-compiled javascript function source code) to the browser and also send JSON data to the browser and let the browser render the jade template plus JSON data into HTML to be inserted into the DOM. Both are possible. Neither are prescribed by express or jade. It's your choice.

Categories