Does the free tier of Heroku apps support file upload? - node.js

I am testing out a MEAN stack application. I have a Node.js backend which handles file upload (via fs/multer). However I'm receiving a 503 error when trying to hit it in production.
Locally everything is working fine. I am using Heroku's free tier; is that tier locked to not allow file upload?
The upload I'm trying currently (have tried several) is only 36kb (.png).
Went through my logs and found the 503-
2018-10-26T20:39:24.352297+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/media/upload" host=www.example.co.uk request_id=5388711c-bc99-4e42-9dbd-7b645fbefb43 fwd="90.222.69.255" dyno=web.1 connect=1ms service=130ms status=503 bytes=0 protocol=http
2018-10-26T20:39:24.306975+00:00 app[web.1]: (node:20) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
2018-10-26T20:39:24.307037+00:00 app[web.1]: (node:20) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
2018-10-26T20:39:24.318818+00:00 app[web.1]: fs.js:113
2018-10-26T20:39:24.318822+00:00 app[web.1]: throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
2018-10-26T20:39:24.318824+00:00 app[web.1]: ^
2018-10-26T20:39:24.318826+00:00 app[web.1]:
2018-10-26T20:39:24.318828+00:00 app[web.1]: Error: ENOENT: no such file or directory, mkdir './uploads/18'

File uploads shouldn't generate HTTP 503 on their own. You should check your logs (via heroku logs) to get more information about what's failing. Having said that, even if you resolve this issue you'll probably need to make a fundamental change with the way you're handling file uploads.
All Heroku tiers have an ephemeral filesystem. Any changes made to the filesystem are lost whenever your dyno restarts, which happens frequently (at least once per day).
The official recommendation is to put uploaded files onto a third-party service like Amazon S3. The multer-s3 library should be able to help with that.

Related

Heroku nodejs app gets 503 error when connecting to heroku-redis

I have a nodejs API app running on heroku and i get a service unavailable 503 error when this app tries to connect to heroku-redis or another redis server on redislabs.com (I tried both redis servers to see if it'd make a difference). I have a free heroku dev account being used for this nodejs API app and the heroku-redis add-on. Perhaps I need a static IP for the free heroku app to connect to heroku-redis? Not sure.
This is what i see when i look at my app's logs on heroku:
2022-02-05T18:30:24.003600+00:00 app[web.1]: > start
2022-02-05T18:30:24.003601+00:00 app[web.1]: > node app.js
2022-02-05T18:30:24.003601+00:00 app[web.1]:
2022-02-05T18:30:26.254765+00:00 heroku[web.1]: State changed from starting to up
2022-02-05T18:31:03.724181+00:00 app[web.1]: Starting RXNsetup app
2022-02-05T18:31:03.725096+00:00 app[web.1]: Start Redis connection
2022-02-05T18:31:33.716830+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/run" host=rxnsetup.herokuapp.com request_id=c983d257-8773-4fff-a4d1-4b74341366b6 fwd="190.111.231.94" dyno=web.1 connect=0ms service=30176ms status=503 bytes=0 protocol=https
2022-02-05T18:30:00.000000+00:00 app[heroku-redis]: source=HEROKU_REDIS_CRIMSON addon=redis-symmetrical-62029 sample#active-connections=1 sample#load-avg-1m=1.105 sample#load-avg-5m=0.96 sample#load-avg-15m=0.6 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15619136kB sample#memory-free=10625808kB sample#memory-cached=2390936kB sample#memory-redis=333744bytes sample#hit-rate=1 sample#evicted-keys=0
The first log statement above is in reference to another nodejs app that makes a POST HTTP request to the nodejs API app being run on heroku. I am able to connect to my redis server fine when i hit this same nodejs API app locally, but for some reason I get a 503 error when the same nodejs app runs in heroku. I have configured all of the same environment variables in heroku like i've done locally. I've been researching for awhile but am stuck with what could be causing this 503 error.
Update:
The root issue looked to be first that I had multiple instances of heroku-redis running, so I needed to promote one instance to be the primary one. Then another issue was that the REDIS_URL environment variable was being evaluated to undefined at runtime which was causing the 503 service errors I was getting.
I suspect you're looking at a stale connection string.
HEROKU_REDIS_<color> variables are normally only used if you have multiple deployments:
Heroku creates the REDIS_URL config var to store the location of the primary instance. In single-instance setups, your new instance is assigned a REDIS_URL. In cases where REDIS_URL exists, your instance is assigned a HEROKU_REDIS_<color> URL instead.
Normally, even if you have multiple instances, you would still connect to REDIS_URL, not HEROKU_REDIS_<color>. You can promote a HEROKU_REDIS_<color> URL to REDIS_URL by running
heroku redis:promote HEROKU_REDIS_<color>
Update your application to use REDIS_URL, review your addons to see if you have multiple Redis services provisioned, and decide which one(s) to keep. Then promote its variable to REDIS_URL as above.

Node Express server works in Heroku staging pipeline but not on production pipeline (no such app error)

I developed an api with nodejs, express and run (npm start) with pm2 (link). I published the app on heroku in a pipeline, I use the staging and production pipeline. The staging app works fine, but the production app is very inconsistent, succeeding sometimes and failing most.
Below the api access log, in the second request there is an error 404 with "no such app" in the html body error message (it is not in the image log).
2021-11-22T12:52:37.266486+00:00 app[web.1]: Starting server in port 12080
2021-11-22T12:52:37.567394+00:00 heroku[router]: at=info method=POST path="/token" host={hidden}-production.herokuapp.com request_id={hidden} fwd="{hidden}" dyno=web.1ect=0ms service=692ms status=200 bytes=613 protocol=https
2021-11-22T12:53:12.688593+00:00 heroku[router]: at=info method=POST path="/token" host={hidden}-production.herokuapp.com request_id={hidden} fwd="{hidden}" dyno=web.1 connect=0ms service=35ms status=404 bytes=711 protocol=https
I believe it is not a route problem, it works sometimes and in staging it works fine. I'm using Heroku's promotion button (staging -> production).
Is there a problem with Heroku?
Are any different configurations required for the application in production?
The problem was in my app syncing with the Heroku routers.
Was fixed by the Heroku team itself.

Heroku H12 Request Timeout Error on Express route executing Python script

I'm running into this major issue on my Heroku app: I have an Express route set up
as follows:
app.get('/recommendations/:data', (req, res) => {
let dataSplit = req.params.data.split("");
let dataInt = [];
dataSplit.forEach(char => dataInt.push(parseInt(char)));
function sendPrediction(dataInf) {
const process = spawn('python', ["./predict.py", dataInt]);
process.stdout.on('data', (data) => {
let jSonned = JSON.stringify(data.toString('utf-8'));
let chopped = jSonned.slice(1, jSonned.length - 3);
res.send(chopped.split(','));
});
}
sendPrediction(dataInt);
});
When I make the GET request to this route from the frontend component, it times out with an H12 error after 30 seconds as per Heroku's default behavior. Since this route works fine on my localhost, it leads me to believe that there is something wrong with the Python dependencies, as I can replicate the same behavior on localhost by deactivating my virtualenv. I know this isn't much to go by, but if anyone has seen something like this and can help I'd greatly appreciate it.
How can I figure out where the issue is?
Here is the error from Heroku logs:
2018-04-30T17:17:27.175201+00:00 heroku[router]: at=error code=H12
desc="Request timeout" method=GET path="/recommendations/41231" host=what-
doxd.herokuapp.com request_id=2249d6ac-a389-40c1-8f64-8947796494a7
fwd="12.23.56.98" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0
protocol=https
Issue resolved: I needed to specify Python 2 and not let heroku default to python 3
First of all, you should attach your heroku logs to the question, so that we can see what Heroku is complaining about.
In absence of Heroku logs, I am guessing that you might not have a python buildpack defined for your app. Since your web dyno is built with node/express, you presumably already have a node.js buildpack (which Heroku may have added to your app automatically for you, if you didn't add it explicitly).
In that case, you need to add a python buildpack. Otherwise, python is not included in your app's slug.
See here for instructions on adding another buildpack to your app.

Heroku is giving me a 503 when trying to open my web app, works on local host

I am trying to deploy to heroku but get a 503 even though it runs on localhost. I am curious as to if my server is set up correctly as I am newer to programming. I hope anyone can point me in the correct direction as to where to look or provide a suggestion as I have spent countless hours on google to this point spanning a couple of weeks.
My main question is if I have set up my server correctly? I am not sure my listener will work for heroku and my .get are used for debugging on localhost when it was initially set up.
Also my full project is available here:
https://github.com/dirkdir/DerekDevSite
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/json', function(req, res) {
console.log("GET the json");
res
.status(200)
.json( {"jsonData" : true} );
});
app.get('/file', function(req, res) {
console.log("GET the file");
res
.status(200)
.sendFile(path.join(__dirname, 'app.js'));
});
var server = app.listen(process.env.PORT || 5000), function() {
var port = server.address().port;
console.log("Express is working on port " + port);
});
Logs:
2017-04-24T20:04:43.755866+00:00 app[web.1]: at Module._compile (module.js:542:28)
2017-04-24T20:04:43.755867+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10)
2017-04-24T20:04:43.755868+00:00 app[web.1]: at Module.load (module.js:487:32)
2017-04-24T20:04:43.755868+00:00 app[web.1]: at tryModuleLoad (module.js:446:12)
2017-04-24T20:04:43.755869+00:00 app[web.1]: at Function.Module._load (module.js:438:3)
2017-04-24T20:04:43.755869+00:00 app[web.1]: at Module.runMain (module.js:604:10)
2017-04-24T20:04:43.755870+00:00 app[web.1]: at run (bootstrap_node.js:393:7)
2017-04-24T20:04:43.755871+00:00 app[web.1]: at startup (bootstrap_node.js:150:9)
2017-04-24T20:04:43.846556+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-24T20:26:31.826133+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=derekdyerdev.herokuapp.com request_id=609ef253-0a56-41ac-b877-1fb242f6f4e1 fwd="69.36.89.218" dyno= connect= service= status=503 bytes= protocol=https
2017-04-24T20:26:32.319732+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=derekdyerdev.herokuapp.com request_id=f2a34e62-9765-
You have the bracket on line 34 of your app.js file in the wrong place.
Since the function is a callback, it needs to be within the params.
Change that last block to this:
const server = app.listen(process.env.PORT || 5000, () => {
const port = server.address().port;
console.log(`Express is working on port ${port}`);
});
I also modified the Procfile to just this:
web: node app.js
After I modified that I was able to run locally, and I deployed it to my Heroku just to test, and it works fine :)
This may be helpful to someone.
My initial Profile content was this :
web : node server.js
and after changing it to this, it worked :
web:node server.js
The space between 'web' and ':' symbol was the problem. Weird though
I had a similar issue. Worked on localhost.. not when deployed to Heroku. My problem? I had cloned a project (node_modules too) and my package.json did not have express, minimist etc.. so it obviously failed.
Tip: When deploying, view the logs right away so you can see what's happening throughout the deployment process. Viewing the logs after it has failed will only show you the end of the logs...
Lastly, ensure you listen on the proper port. I do this (as others do too)
var serverPort = 8080;
. . .
var port = process.env.PORT || serverPort;
I just had the same error on my heroku app.
I installed cors locally and pushed only the file app.js to the repository and deployed it to heroku.
The error was actually that the cors package wasnt declared on my package.json (I only pushed app.js to github) so when heroku tried to call the app it caused an error because cors was undefined.
The point is, also check if the package.json is updated.
This Might help others
From Terminal Check out your Heroku Logs
Check out the error message
This might be possible that any npm module you have installed globally
Then just install that npm module locally.
Commit the updated package.json and package-lock.json file on git.
Then run git push Heroku master.
I had to do a couple of things before I could get my app running, I will list all of them in case it helps anyone.
1.) Make sure you specify the version of node in your package.json file like this
"engines": {
"node": "14.15.1"
}
2.) Make sure you have no errors in your Procfile, the below command worked for me
web: node server.js
3.) My server.js file was inside the src folder and that was the root cause for me, I had
to move it out of that folder and then it started working for me
I was struggling with similar problem then noticed quotation marks in my Procfile. When I removed them problem solved.
"web: node index.js" WRONG!
It must be without quotes:
web: node index.js CORRECT!
I had this same issue when deploying a Rails 6 API only application to Heroku.
I was getting this error when I try accessing the application in a browser:
Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail
Here's how I fixed it:
I ran the command below to check the application logs:
heroku logs --tail
Then I scrolled up within the logs to view logs from when the application server was started. Then I found this error:
2020-12-17T12:28:46.973736+00:00 heroku[web.1]: Process exited with status 1
2020-12-17T12:28:47.017905+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-17T12:28:47.028049+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-17T12:28:50.252842+00:00 heroku[web.1]: Starting process with command `bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-production}`
2020-12-17T12:28:53.969375+00:00 app[web.1]: Puma starting in single mode...
2020-12-17T12:28:53.969443+00:00 app[web.1]: * Version 4.3.7 (ruby 2.6.6-p146), codename: Mysterious Traveller
2020-12-17T12:28:53.969472+00:00 app[web.1]: * Min threads: 5, max threads: 5
2020-12-17T12:28:53.969505+00:00 app[web.1]: * Environment: production
2020-12-17T12:28:57.565417+00:00 app[web.1]: ! Unable to load application: SyntaxError: /app/app/policies/user_policy.rb:4: duplicated argument name
2020-12-17T12:28:57.565428+00:00 app[web.1]: def initialize(user, user)
2020-12-17T12:28:57.565429+00:00 app[web.1]: ^~~~
2020-12-17T12:28:57.565468+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.6.0/bin/puma)
2020-12-17T12:28:57.565510+00:00 app[web.1]: SyntaxError: /app/app/policies/user_policy.rb:4: duplicated argument name
2020-12-17T12:28:57.565510+00:00 app[web.1]: def initialize(user, user)
2020-12-17T12:28:57.565511+00:00 app[web.1]: ^~~~
I fixed the error in my code which was caused by a duplicated argument name, that is def initialize(user, user) in my Pundit gem policies.
In the policies file for Users (app/policies/user_policy.rb) in my application, I simply renamed:
def initialize(user, user)
to
def initialize(user, user_model)
and almost did the renaming at the other places in the app/policies/user_policy.rb file that user, user was defined to user, user_model.
I saved and then pushed it to production and that fixed the issue for me.
That's all.
I have run app on local environment. In the log I saw "Module can not found 'index.js'" I updated my main js "app.js" to "index.js" and update package.json for main js. Then push my code to Heroku again. it worked.

Weinre on heroku

I am trying to set up Weinre on heroku but I am having some issues.
I manage to build the app, simply by using a package.json with weinre npm dependency and a Procfile that looks like the following
web: node_modules/weinre/weinre --httpPort $PORT
At first everything seems to work, the client shows up the UI, the JS file loads fine, but the debug does not work and no client ever shows up at the server side.
While looking at the logs, I realized that I keep getting 404 errors for every request that contains /ws/target/* or /ws/client/*
2012-05-31T22:37:53+00:00 heroku[router]: GET xxxx.herokuapp.com/target/target-script.js dyno=web.1 queue=0 wait=0ms service=10ms status=200 bytes=190900
2012-05-31T22:37:36+00:00 heroku[router]: GET xxxx.herokuapp.com/ws/target/t-3 dyno=web.1 queue=0 wait=0ms service=3ms status=404 bytes=9
2012-05-31T22:36:39+00:00 heroku[router]: POST xxxx.herokuapp.com/ws/client/c-1 dyno=web.1 queue=0 wait=0ms service=40ms status=404 bytes=9
This setup works localy with no problem.
I also tried the Procilfe with --boundHost 0.0.0.0 and got the same error. when I use my heroku app domain/subdomain as the host I got weinre: error running server: Error: listen EADDRNOTAVAIL error
If needed, you can checkout the source at the github repo
https://github.com/felipesabino/weinre-heroku
EDIT:
It is possible that it is related to WebSockets not being supported on Heroku:
https://devcenter.heroku.com/articles/http-routing#websockets
The weirdest thing is that the same issue happens with PhoneGap's weinre debug page as well
http://debug.phonegap.com/
Does anybody know how to solve this?
I have weinre running on Heroku at the moment but I remember that when I was trying to get it working it was a real nightmare.
This was using the 2.0.0-pre-H0WVARLU-incubating of weinre so it may be a bit different but what I had to do was:
Modify weinre itself, I had to tell weinre to bind to all hosts, I modified the cli.coffee file and I set the boundHost to -all- as the default
I commended out the if remoteAddress check in channelManager.coffee since the address you come in from is remapped through the proxy and was never matching
Turn on verbose debugging, while you don't actually need this to run it made it a lot easier to trace where errors are happening (and why the requests weren't routing)
My Procfile ended up being very simple as I tried to use weinre as "native" as possible (note the repo contains only weinre, it's not a module like you're doing):
web: node weinre
I left any port changes to be handled by process.env.PORT, not passed in from the Procfile
Basically what I found was that weinre really isn't designed to work behind a proxy!
See this pull request https://github.com/apache/incubator-cordova-weinre/pull/10
Note that no other modifications are needed, Procfile can set the options (like it should) and Weinre is properly used as node module.
package.json (that contains the fix)
"dependencies" : {
"weinre": "https://github.com/downloads/AppGyver/incubator-cordova-weinre/apache-cordova-weinre-2.0.0-pre-H77LBWIW-incubating-bin.tar.gz"
},
Procfile
web: node_modules/weinre/weinre --httpPort $PORT --boundHost -all- --debug true --verbose true
Modify weinre itself, I had to tell weinre to bind to all hosts, I modified the cli.coffee file and I set the boundHost to -all- as the default
I commended out the if remoteAddress check in channelManager.coffee since the address you come in from is remapped through the proxy and was never matching
this two Hints helped a lot on appfog, too
Also you need to
rename weinre to app.js
require local coffeescript
assign cli to a variable and execute run direct
extend cli.coffee - > Port with process.env.PORT

Resources