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.
Related
I have written some code in node.js that is expecting a whole slew of unique id's in the route.
it looks something like this
api/389+138+638+659+665+814+148+713+730+834+241+77+682+802+173+661+695+192+809+733+644+272+675+735+76+656+660+757+144+745+628+593+624+787+788+789+129+668+810+630+474+673+716+36+837+771+203+725+169+133+655+103+636+731+11+300+813+417+742+799+803+794+755+812+429+387+75+831+830+451+163+835+642+734+817+844+696+187+286+363+613+750+822+807+292+38+671+710+793+437+683+676+649+648+392+712+711+702+801+653+754+806+597+843+140+643+740+773+394+223+294+48+239+792+827+824+826+815+828+825+795+309+805+838+335+722+412+749+763+301+634+820+821+819+818+833+785+720+718+719+744+743+631+782+753+796+847+832+736+645+641+196+848+27+421+748+737+777+778+172+457+625+780+845+666+433+574+577+368+63+846+633+623+411+249+640+762+791+410+770+797+727+377+449+839+840+237+709+751+829+694+219+229+841+800+647+81+674+376+114+444+685+407+432+431+403+760+678+579+836+752+408+586
I get a 400 -- bad request back every time.
But, if I shorten the list significantly like this it hits the API with no problems.
api/429+387+75+831+830+451+163+835+642+734+817+844+696+187+286+363+613+750+822+807+292+38+671+710+793+437+683+676+649+648+392+712+716
Is it possible for a request to be too long? It may also be worth noting that this is not an issue at all when I'm developing locally. It only throws it back at me when I'm in one of our deployed environments.
Is there a better way to make this request or is there some kind of node, server, or application setting that can be adjusted?
Yes, it is possible. You need to increase the size in your web server (ex: nginx, apache)
Add this to your server block for nginx
client_max_body_size 2M;
For anyone else who stumbles across this, it seemed like the only way to change this was to get into the IIS register. I didn't want to do that because this for a number of reasons. So instead, I changed my code to have to route be a POST instead of a GET and sent all of the info in the header. This may not fix the issue 10 times out of 10, but it took me like 20 minutes to reconfigure everything on the front and back end and it works fine.
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?
Long time reader, first time question asker, please be gentle.
I'm after a simple technique to display a simple message to users that I can easily edit and change. Stored on the server so when the client connects they will get the latest message. I'm using Heroku with a simple node.js server.
I'd rather not store it in a database (mongo etc) as this is overkill and I'm not using a db for any other reason.
I don't want to store it in the server code as this seems a bit cumbersome to redeploy the server to Heroku whenever the message changes.
Maybe it's Monday morning and my wife has swapped my coffee for decafe but any guidance greatly appreciated.
I have built a node.js app with this structure:
In app.js:
var myList = ['0'];
app.get('/webpage',function(req,res){
console.log(myList);
res.render('webpage.ejs', {exps: myList});
});
On "webpage" I can display myList and there is also a form that allows me to add elements to myList. Let's say I append '1' to myList through this form.
I have the following problem I don't know how to debug:
locally on my computer, this app works fine: I can see ['0','1'] in my console each time I refresh "webpage".
online on Heroku, when I refresh "webpage" sometimes I see ['0','1'] sometimes I see just ['0'] and a couple of refresh later I see ['0','1'] again: it is like myList randomly oscillates between its default value when the app was first launched and the value that was specified later.
I use the same npm and node versions locally and on heroku and the same versions of dependencies. To my knowledge I have the same environment locally and heroku, so I have no idea where this problem could be coming from.
You may be running multiple instances of on Heroku, in which case each request might be assigned to a different instance, each with its own process and memory space.
I believe Heroku also shuts down instances after a period of inactivity, so that might be an issue too.
If you intend to persist something, how about storing it in a database?
I am completely stumped by what I noticed today in my app.
I have an app written in Node.js running on nginx with MongoDB in the backend. I have an 'authenticateUser' cal which takes in a username and password. It then queries MongoDB to retrieve the users document and checks if the password matches up.
We wrote a script which basically calls 'authenticateUser' in a loop 100 times. It worked fine. No errors. Now we ran the same script from 2 terminals, one for user bill and the other for user sam. We started seeing failures on both the terminals. I would say about 10% of the requests failed with invalid password errors.
When we inspected the log files, we were completely surprised to see bill's username getting mixed up with sam's password. We have no idea what's going on. We must be doing something obviously wrong. What is it? Aren't the two requests completely isolated from each other?
Do you use global variables often? Missing var is a common cause of such errors.
And yeah, i'd like to see a source code...