Document download counter? Using Node.js, express, mongo, mongoose - node.js

I'm working on a medium size company's intranet. The website is hosted on-site, and will have many links to documents hosted on the same server.
Does anybody know what's the best/easiest way to keep count of downloads of each document?
Website developed using Node.js, express, mongo, mongoose.

Simplest thing to do would be to just have a mongo document holding the metadata about each file, and then increment the “downloads” field of that every time it’s downloaded.
Slightly harder but probably more useful would be to log info about each download either in its own mongo record or to the system logs. In that case you’re going to capture things like which user, when, etc, which you’d then count to get the total, but you could also do things like see which user did it or more complex things like which department is doing the most downloading, etc.

To keep track of all that are using your file, that you can do is that keep a logger as middleware in express as a result you will be a find who all have access to file by searching the log. morgan is a good logger which helps you this.

Related

Storing data temporarily in nodejs application

I'm developing a nodejs back-end application which will fetch data from third-party Hotel API provider based on the user input from the Angular application. User should be able to filter and sort the received data like filtering price, hotel rating and sorting price, hotel name etc. but unfortunately API doesn't support this. So I thought to store that data in nodejs temporarily but I'm not sure what's the right approach. Will Redis support this?. A good suggestion will be really appreciated.
Redis should be able to support something like this. That or you could do all of the sorting client side and save all the hotel information in local or session storage. Either route you go with you'll need to make sure to save the entire response with a unique key so that it is easy to fetch, or if you save individual values to Redis make sure each has a key to query against. Also keep in mind Redis is best for caching information for short term periods rather than long term solutions like PostgreSQL and MySQL. But for just temp responses, it should be a fine approach.

Best Solution to work with Dates in React/Node/Mongo/Mongoose app

I work on the following tech stack.
React - for frontend
Node/Express - For Middle Tier
MongoDB - database
Mongoose Driver - to connect the express app to Mongo DB
I use some date picker components to display dates in the UI, which the users can change. I would like to know what is the best way to post dates to the server, and persist in the DB and likewise, how to query for collections with the date formats using mongoose in the express app.
Please suggest consistent and best possible solution.
You have plenty of options for dates. You can stay super lean and deal with them yourself via JS Date object or use momentJS and its tons of plug-ins or go fully functional and light with date-fns.
All of these can be used on the client and on the server. momentJS / date-fns have tons of convenient methods that would save you a lot of time and headaches. date-fns can also be partially imported so you do not need the entire lib if you only want few methods.
Saving the dates ... the recommended way is to use the default ISODate for mongoDB when persisting them (which is mongoDB default anyway). Saving them as strings is not really a recommended thing but you could and you would still be ok as long as you are on version 3.6+ since there and above they added tons of date/string manipulation functions which would save you tons of time if you have to go back and forth from string to date and vise versa. It is best however to deal with dates from the get go.
You should use Date.now() (epoch or utc) both on the server side and client side js. Date fns is a good light-weight js library that works both in node and react js.

How to fetch from nodejs-api-starter into react-starter-kit

I am trying out React-Starter-Kit for the first time and loving all the cutting edge features baked in (apollo/graphql-client in particular). A crucial part of any app for me is the database, and for that my understanding is the same author provides nodejs-api-starter which sets up a REST interface for accessing Postgres at localhost:5000 and has a graphql webui at localhost:5000/graphl.
That is about as far as I have been able to understand of the setup so far. I have changed the frontend code a little bit so a new Component "Counter" is loaded on the home page. I need to be able to make a new counter, fetch the latest counter, and increment decrement the counter. Write now the component just outputs the 'value' retrieved from the server at 5000.
I do not think I am accessing the 5000 server correctly, do I put the port in this url line somehow?
You can pull the repo down from : https://github.com/Falieson/react-starter-kit-crud-counter-demo
This is my first time setting up a nodejs api server, I am used to using MeteorJS which has pub/sub to MongoDB baked in. I am looking forward to the separation the RSK strategy (which seems more industry standard?) provides.
I've just done setting up the full site with Database from React-Stater-Kit, I'm also a newbie so I understand your frustration.
About this question, you don't need the NodeJS-API-Starter, it has enhanced function ( such as Redis cache ) and it's not suited for newbies. You should look deeper into the RSK, it already has the DB. If you ran the boilerplate and played around, change is you'll see file database.sqlite in your folder, it's the database. Here are the things you should learn:
Use SequelizeJS to connect the NodeJS server with database. Your database can be MySQL/MariaDB, PostgreSQL or SQLite. The connection is easy and there's tool to auto-generate Models from your database
How to create GraphQL's Types and Queries. If your queries need to search through the database, import Sequelize's models and use its functions.
Test your API via GraphQLi
Note: if you want to use MongoDB or other NoSQL, try Mongoose instead of Sequelize.

mongoose: send data back without saving

I just started using node, backbone and mongoose not so long ago to create my first web app.
At the very beginning, I followed tutorials, and used backbone client side to define models. Those models mirror my mongoose schemas server side.
When I run schema.save() on one of my models, my data is automatically sent back to the client, with an _id.
But now that my app is almost finished, I realize that I don´t really need to save anything, as the only thing I do is query an api, and the data doesn´t have to be reused.
So my question is, what is the best way to keep the same mechanisms, but without saving anything?
The end reason is that I plan to run the app on an ec2 instance, and knowing that I don´t need to save anything, I think it is more beneficial to reduce the IO usage by not having any database.
Thanks, and sorry if the question seems dumb.

Is this possible with node.js? (JSON, SQL and pushing changes)

I have a website which can have up to 500 concurrent viewers, with data updated every three seconds. Currently each user has an AJAX object which calls a web-page every three seconds which queries a DB and returns with the results.
What I would love to do is have each client get a socket to a node.js object, this node.js would poll the DB every 3 seconds for updated data, if it had updated data it would then be announced (ideally through JSON) and each client would then have the data pushed to it and update the page accordingly.
If this is possible, does anyone have a recommendation as to where I start? I am fairly familiar with JS but node.js seems to confuse me.
Thanks
I myself have quite few experience with node.js.
It is absolutely doable and looks like the perfect use case for node.js.
I recommend starting with an Express Tutorial and later on use socket.io.
I don't know which DBMS you are using, but there probably is a nice package for that as well. Just look through this list.

Resources