Real-time stores stock MVC application using redis - ejs - express.js - node.js

I am building a MVC application of a real-time stores stock application, where: the model is redis, the views is the template engine - ejs, and the controller is express.js.
I have built already the routes and the views, but I find it's hard to define the model.
In the model package I have written the connection to the redis local docker server, as well as some functions that will return the stock amount of a product based on the store name and the product name and another function which return the list of stores name. This data is crucial for some charts I am updating in the ejs files. Therefore, in a certain ejs file I am rendering the page with multiple data in the json. For example, I have a store.ejs file which requires the stores name and also the products amount. Hence, in the store.js controller file I am rendering as following:
res.render("pages/stores", {storesName: storesName, productsAmount: productsAmount})
Before rendering the page I have to extract the data from my redis server, however, since these get redis methods return a promise, I can't really extract all of them but only one - making only get request from the redis server. So I am sure that this isn't the correct way to do so.
Therefore, I will be glad to see what you think about that.
Thanks!

Related

Node.js: Is there an advantage to populating page data using Socket.io vs res.render(), or vice-versa?

Let's say, hypothetically, I am working on a website which provides live score updates for sporting fixtures.
A script checks an external API for updates every few seconds. If there is a new update, the information is saved to a database, and then pushed out to the user.
When a new user accesses the website, a script queries the database and populates the page with all the information ingested so far.
I am using socket.io to push live updates. However, when someone is accessing the page for the first time, I have a couple of options:
I could use the existing socket.io infrastructure to populate the page
I could request the information when routing the user, pass it into res.render() as an argument and render the data using, for example, Pug.
In this circumstance, my instinct would be to utilise the existing socket.io infrastructure; purely because it would save me writing additional code. However, I am curious to know whether there are any other reasons for, or against, using either approach. For example, would it be more performant to render the data, initially, using one approach or the other?

How can I save client-side content-editable updates to mongodb database in node.js express app

What is the best way to persist user-generated data with NodeJS, Express, and MongoDB?
I'm building my first web-app using this stack (with bootstrap HTML/CSS and JS for the frontend) and I've realised that I need a data-binding solution. I'd rather avoid a complete rebuild of my front-end so it seems like React will be the best option, but I'd rather find out now if I'm missing something obvious.
The app will allow users to create 1-n documents, generate 1-n new components within them, and edit 1-n content-editable elements within those components.
I'm at the point where I've built the server, db, and frontend and the users and documents persist, but the components and their content does not.
The functionality I would like is that, when a user generates a new element or exits the contenteditable area of that element, any changes they have made will persist. I'd like to achieve this without a bazillion API calls.
Any assistance appreciated.
You may create page description in markdown and then render it to react components.
For example you can check https://www.gatsbyjs.org/ plus Remark Custopm blocks plugin - https://www.gatsbyjs.org/packages/gatsby-remark-custom-blocks/

how to use cache data in mvc architecture

As the title suggest. I'm trying to figure out where I should cache data in my node.js application.
I'm using a express.js and controllers to handle the routes in the application. The controller for a particular route will get data via the model layer using REST API and then it uses handlebars for the view rendering on the server.
For this particular route, I'm displaying a menu and the data I have got for this has been done in the model and a remote REST call.
When the user select different items in the menu, I do not want to make a new REST call to get the same data for the menu again, I just need to get the data for this menu once since it will never change.
I need to find out a way to cache it, but do not know where I should implement it?
Best Regards
You could just cache the response from the REST API or DB lookup using a memory-store like Redis or Memcached, both have good modules available on npm - (Redis, memcached).
You would need to attempt to fetch the data from the memory-store (in your controller), if no matching data was found, you would make the request to the API or database to get the data, and then store it in your chosen memory-store so future requests will hit the cache.
note: There are also some pure JavaScript caches available such as memory-cache or lru-cache if you don't want to add an additional application.

Website Architecture for Node, Backbone+Marionette, Mongo DB GridFS

I am programming a website with a node.js and mongodb grifs on back-end with backbone and marionette as front end frameworks.
As I am new to creating single page applications (SPA) I may be wrong in any of my assumptions, please correct me wherever I am wrong.
The way I understand the flow of control is:
As soon as the first request to the server is made to abcxyz.com, nodejs server serves the base html file via a get request from the server.js file. This html file links to the main.js file which holds all the backbone+marionette models, collections and views.
As my aim is to write a SPA (single page application), I assume that from here onwards the flow of control remains completely within the main.js file as it is the file which handles the url routing (think of #routes for emails in gmail).
Now my question is this: How can I carry data to and from the mongodb gridfs database for each route change in backbone. Putting another way - how does backbone interface with mongo database which can be accessed only in the server.js file, and how do I communicate a change of route (hence a change in the data needed for backbone views) from backbone to the node.js server?
I'm sorry for a really long question, feeling very confused :(

Sail.js - how to structure JSON based live data output with existing static data in the model

In my Angular app, I want to display a table which contains the following
a) URL
b) Social share counts divided by different social networks
Using Sails.js, I already have the api created for the URL when the results show up, I can display the URL now I'm confused how to get the appropriate social counts showing right besides
Here's the API I'm using: https://docs.sharedcount.com/
by itself, I can see the JSON it produces
But here are my questions:
Should I create a new api (model/controller) for social count data or include it in my model where I have the 'url' action defined?
If I create a new api or include the social_counts as an action in the current, what would my JSON query look like? to retrieve the URL's, I'm using default API blueprint that Sails provides, so:
http://www.example.com/url/find?where={"title":{"contains":"mark"}}
Struggling a bit in terms of the thought process, would be great to get input on this
It depends on your app. is your app will store that data or just consume it? If it need to store, of course you need the API. In purpose for modification or aggregating the data for example.
No, you can't do that. That shortcut method only works if you have the data in your database and let the Sails Waterline ORM and Blueprint API served it.
Perhaps, if you only need to consume the data from that Sharedcount API, you didn't need to use Sails as a backend, in this context. Just use Angular as a client of that API. Except if you need to modify the data first and store it in your own database, so Sails will helps with it's Waterline ORM and Blueprint API.

Resources