Session Values Fails to Load on Request but Loads on Next Request - node.js

As a newbie to node, I am getting more confused for several days now, and I think I can't withstand the stress anymore. So I think my only way out is to post my challenge here after goggling and attempting any means I can without any positive result.
I am using express with node, together with the express-session and redis to build my app. Meanwhile, I am using Mysql database to store my data, and redis to store sessions. Everything seem to be working normal, but the issue has got to do with my session variables. When I store a value in session, and try's to retrieve it, it won't load, meanwhile, the value is output perfectly in the console. However, when I try to request something else, then the previous session value is displayed. So is like previous sessions gets loaded on current request. I am using handlebars as my template. I just don't know what I am doing wrong, but all the same, this is strange. As I pointed out, previous session value loads on next request or say current request loads previous session values. Any suggestions please?

Related

How do I save data across a node.js server?

I'm using Heroku to host a node.js server where a variable that stores the number of times every user that used the site has clicked something on it. When clicked, the variable gets increased by 1. However, Heroku does this thing where inactivity for 15 mins causes the site to go to sleep and everything is reset. I tried to use node.js to write to a file and save it but it seems the files are also reset. Does anyone know a way to get the data saved even after Heroku declares it inactive?
There is no way around it, since Heroku gets rid of files after inactivity. You need some external storage like a MongoDB set up somewhere else.

Node.js application not rendering mongoDB collection after model.find()

I'm experiencing an issue in my node application with accessing the contents of a mongoDB collection via mongoose.
this problem only ever happens the very first time my application makes a call to the database collection. what should occur when the user presses the submit button, is that the contents of the collection should be added dynamically to the DOM. However, this does not occur. but when I check the collection via the command line interface, the user submitted data is there, and then when I press the refresh button on the browser (which hits the same route the user was redirected to upon initially submitting the form) the data renders to the page like it should have the first time.
When I noticed this, i tried starting from scratch. I dropped my database, restarted my server, but this time, I used robo3t to create the db and the collection before I started my app and filled out the form data, and viola, when I submit my data and hit save, the information gets saved to the collection AND rendered the the page successfully the first time.
So i went and looked at the different Model methods provided by mongoose and thought maybe I just needed to add a function call to initialize the collection sometime before calling Model.find() but everything i'm looking at tells me that the collection is already (obviously) initialized when you call mongoose.model()
so i'm a little confused about what to do in order to make sure my data gets rendered to the webpage the first time the user submits information, rather than after refreshing.....i've been careful about awaiting all the async functionality but maybe I missed something?
the repo is here in the unlikely case someone wants to clone this and try to recreate the situation. Let me know if there is more information I can provide
https://github.com/Funkh0user/TripCheq-Travel-Assistant
Thank you.
I Solved it. I needed to rearrange some things such that the block of code responsible for doing some computations and then sending the result back to the application was in the callback for model.find()
Thanks!

at server i keep getting socketio request constantly every 2 seconds, but google analytics show no one is here.

I've removed all socketio code but someone either hasn't uploaded page for days or something else. But for some reason server is getting bombarded with socketio request which are failing because i removed all the code both on client and server. However, they are still coming. ??? what can i do. Block ip?
I can't change webdomain name. Which is given. I can't think of any options, they're coming from like 6 different ips. They would have been legit requests some weeks ago. but not now.
Are you worried that handling these requests will impede your server's performance? The only legitimate reason I can think of is that someone's browser cache hasn't been cleared properly since the update, assuming you enabled caching on your express server.
If your intention is to improve performance, I suggest putting that path high on the express method chain so that the server can end the request as quickly as possible and minimize the load on the server.
If you want the people to become aware that their requests are invalid, you could route the path to a javascript file that redirects the current page to another document. On the document, have directions that instruct the user to clear their browser cache in order to properly update their client.
Hope that helps.

Setting a handlebars helper to return a specific value per request in express

I have an express based app serving server side rendered HTML from handlebars templates, and a bundle of the backbone resources. Theoretically, client side, the app resembles what is happening server side.
This is all fine in development, but when the node server is dealing with many requests at the same time, then the mechanism by which the helper we are using is defined/redefined breaks - we set the helper (in this case logged in / not logged in, but could be anything) then serving the rest of the request happens asynchronously - we don't know and cannot control how long this will take.
I have already figured out that this is because Handlebars on the server is effectively a global - so every time a request comes in, the helper that is being called is from there, a shared object between requests.
The question is, how to be able to set a helper per async request that returns that particular value, and does not get polluted by concurrent requests...?
Here's a gist of a test case - hopefully shows the problem:
https://gist.github.com/dazld/023df6e1da7a92387720
(if it is not obvious from that what i am going for, just ping in comments, and i will write up something clearer).
Thanks!
This is because your using a single instance of Handlebars and with lots of requests your polluting one request with another.
I use hbs (https://github.com/donpark/hbs) as it creates a new instance of handlebars for each request/render for you.

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.

Resources