I have a Node.js application that runs perfectly on my localhost.
However, when i uploaded to Heroku, the server crashes once a portion of the code is executed.
The given code is supposed to extract a portion from a URL string that was passed from a form.
This is the code that causes the server to crash:
function getUserDetails(username) {
return new Promise(done => {
var data = [];
https.get(`https://www.instagram.com/${username}/?__a=1`, resp => {
resp.on('data', chunk => data.push(chunk));
resp.on('end', () => {
var json = JSON.parse(data.join(''));
done(json.graphql.user);
});
});
});
}
After Heroku crashedd - and it stil crashes consistently, i checked the logs with heroku logs --tail.
This is the report from the logs:
2019-10-06T10:59:40.782839+00:00 app[web.1]: undefined:1
2019-10-06T10:59:40.782863+00:00 app[web.1]:
2019-10-06T10:59:40.782865+00:00 app[web.1]:
2019-10-06T10:59:40.782867+00:00 app[web.1]:
2019-10-06T10:59:40.782870+00:00 app[web.1]: SyntaxError: Unexpected end of JSON
input
2019-10-06T10:59:40.782872+00:00 app[web.1]: at JSON.parse (<anonymous>)
2019-10-06T10:59:40.782874+00:00 app[web.1]: at IncomingMessage.resp.on (/app/ap
p/routes.js:108:33)
2019-10-06T10:59:40.782876+00:00 app[web.1]: at IncomingMessage.emit (events.js:
203:15)
2019-10-06T10:59:40.782878+00:00 app[web.1]: at endReadableNT (_stream_readable.
js:1145:12)
2019-10-06T10:59:40.782880+00:00 app[web.1]: at process._tickCallback (internal/
process/next_tick.js:63:19)
2019-10-06T10:59:40.869547+00:00 heroku[web.1]: State changed from up to crashed
2019-10-06T10:59:40.847583+00:00 heroku[web.1]: Process exited with status 1
2019-10-06T10:59:40.790256+00:00 heroku[router]: at=error code=H13 desc="Connect
ion closed without response" method=POST path="/MY_PATH" host=MY_HOST.herok
uapp.com request_id=15118004-c799-45bd-a0a9-909cbd3a5e86 fwd="130.43.125.250" dy
no=web.1 connect=1ms service=101ms status=503 bytes=0 protocol=https
Where are you getting the url value from?
Looks like you are reading in some other place from a json input and thats incorrectly formatted or missing a tag or so.
On running your piece of code locally it works as expected.
var URL = require('url').URL;
url = "stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when this-code-runs";
if(!/^https?:\/\//i.test(url)){
url = "http://" + url;}
let parsed = new URL(url);
let retrieved = parsed.pathname.split('/')[1];
Output :
node v10.16.0
=> 'http://stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when-this-code-runs'
Related
Exactly what it sounds like.
I use ExpressJS for my Node app, which is hosted on Heroku.
Despite using app.listen, it consistently is getting / causing heroku R10 errors, which are caused by a web app not binding to process.env.PORT in time.
The relevant code:
const app = express();
var isRoot = (process.getuid && (process.getuid() === 0));
var port;
if (isRoot) {
port = 80;
} else {
port = process.env.PORT | 8000;
}
const server = app.listen(port, onStartup);
function onStartup() {
console.log("Started webserver on port "+port);
}
Now the odd thing is, I'm getting the "Started webserver on port [foo]" message, it's just not binding to the port.
Logs:
2020-03-30T19:50:39.434302+00:00 app[web.1]: > foo-bar#1.0.0 start /app
2020-03-30T19:50:39.434303+00:00 app[web.1]: > node scrape2.js
2020-03-30T19:50:39.434303+00:00 app[web.1]:
2020-03-30T19:50:39.829882+00:00 app[web.1]: Verbose mode OFF
2020-03-30T19:50:39.830782+00:00 app[web.1]: Started webserver on port 8052
2020-03-30T19:51:37.415192+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-30T19:51:37.293060+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-03-30T19:51:37.293142+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-03-30T19:51:37.391762+00:00 heroku[web.1]: Process exited with status 137
Help!
I did a stupid and accidentally used the bitwise OR operator, which caused it to bind to a different port than process.env.PORT. Changed it from | to || and it works fine now.
I am trying to run the following command "npm start" however it throws the following error:
internal/net.js:17
throw new RangeError('"port" argument must be >= 0 and < 65536');
^
RangeError: "port" argument must be >= 0 and < 65536
at assertPort (internal/net.js:17:11)
at Server.listen (net.js:1389:5)
at EventEmitter.listen (/Users/keyurshah/fulljs/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/Users/keyurshah/fulljs/server.js:10:8)
at Module._compile (module.js:570:32)
at loader (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...
Check server.js line 10. Your port is probably specified there.
If not just declare it to be what you want right before the listen function.
From
app.listen(port, ...)
to
app.listen(8080, ...)
Otherwise you haven't shown any code not sure how we could help you with such limited information...
I recommend you start with more simple projects and get a feeling for how things work: http://expressjs.com/en/starter/hello-world.html
try to do this or change with any port number you like .listen(portNumber)
var http = require('http');
http.createServer( function (request, response) {
//your code
}).listen(8081);
EDIT ; attaching my app.js , I am using
git add app.js
git commit -m "updated app.js"
git push
command to push code from local machine, and my app.js code is as follows :
/*
*RESTfull server
*/
//defining express middleware
var express=require('express');
//require mongoose, this middleware helps in modeling data for mongodb
var mongoose=require('mongoose');
//require passport, this middleware helps in authentiation
var passport=require('passport');
//require passport, this middleware parsing body
var bodyParser = require('body-parser');
var flash = require('connect-flash');
//define port on which node app is gonna run
//var port = process.env.PORT || 8000;
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080 ;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1' ;
var app=express();
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
======================================================================
app.listen(server_port,server_ip_address);
console.log('The magic happens on port ' + 'http://'+server_ip_address+':'+server_port);
EDIT :
I commented all mongodb connection code, now my app.js has simple expressjs code, still I can see the same output from the command "rhc tail -a app", Iam not sure why nodejs catridge is trying to connect to mongodb, eventhough there is no code in app.js, is it possible that the log has been generated previously and the same log is being shown ? can I clear log file and test it once? can somebody please help me.
I deployed my nodejs(expressjs) app to the openshift server. I am hitting a mongoError "MongoError: auth fails", I am providing credentials to mongodb server.
Initially when node child process starts it is trying to connect to the
"mongodb://admin:XXXXXX#ip:port" but it should connect to "mongodb://admin:XXXXXX#ip:port/admin" as credentials reside in admin.system.users collection.
I am using mongoose to connect to mongoDB so I changed my mongoose connect to
mongoose.connect(mongodb://admin:XXXXXX#ip:port/admin); But I still see child process is trying to connect to this url "mongodb://admin:XXXXXX#ip:port", but later point of time it connects to the correct collection, and I can see the console ouput of the following code.
mongoose.connection.once('connected', function() {
console.log("Connected to database G")
});
I tested few routes, they are working fine. I want to understand why is it behaving so and can I ignore this error or how can I resolve this issue??
Thanks in advance.
You should be using process.env.OPENSHIFT_MONGODB_DB_URL instead of forming your own url. This environment variable has the following format:
mongodb://admin:LX3eZCP6yxxx#123e4b9a5973ca07ca00002f-appname.rhcloud.com:12345/
Attaching my "rhc tail -a app" commad,
==> app-root/logs/nodejs.log-20150328020443 <==
DEBUG: Starting child process with 'node app.js'
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
at Object.toError (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runt
ime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
==> app-root/logs/nodejs.log-20150327071155 <==
at EventEmitter.emit (events.js:98:17)
DEBUG: Program node app.js exited with code 8
DEBUG: Starting child process with 'node app.js'
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
==> app-root/logs/nodejs.log <==
DEBUG: program 'app.js'
DEBUG: --watch '/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/data/.nod
ewatch'
DEBUG: --ignore 'undefined'
DEBUG: --extensions 'node|js|coffee'
DEBUG: --exec 'node'
DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory '/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/
data/.nodewatch' for changes.
admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281
The magic happens on port http://127.9.17.129:8080
Connected to database G
==> app-root/logs/haproxy.log <==
[WARNING] 087/140540 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/001408 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/001408 (417258) : proxy 'express' has no server available!
[WARNING] 088/002019 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 29ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/110018 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/110018 (417258) : proxy 'express' has no server available!
[WARNING] 088/110112 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/110502 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/110502 (417258) : proxy 'express' has no server available!
[WARNING] 088/110556 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
==> app-root/logs/nodejs.log-20150328074316 <==
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
at Object.toError (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runt
ime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/var/lib/openshift/550f3c0ffcf933066f0001b8/app
-root/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connec
tion/base.js:453:41)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
==> app-root/logs/haproxy_ctld.log <==
I, [2015-03-22T18:06:38.808186 #415579] INFO -- : Starting haproxy_ctld
I, [2015-03-27T14:20:21.556898 #15736] INFO -- : Starting haproxy_ctld
I, [2015-03-29T12:18:29.365873 #417278] INFO -- : Starting haproxy_ctld
I, [2015-03-29T12:18:37.485326 #417532] INFO -- : Starting haproxy_ctld
==> app-root/logs/nodejs.log-20150323084556 <==
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/
runtime/repo/app.js:43:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
DEBUG: Program node app.js exited with code 8
==> app-root/logs/nodejs.log-20150328012640 <==
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/var/lib/openshift/550f3c0ffcf933066f0001b8/app
-root/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connec
tion/base.js:453:41)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
at MongoReply.parseBody (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-roo
t/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/
mongo_reply.js:68:5)
at null.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/ru
ntime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/ser
ver.js:445:20)
at EventEmitter.emit (events.js:95:17)
at null.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/ru
ntime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/con
nection_pool.js:207:13)
at EventEmitter.emit (events.js:98:17)
DEBUG: Program node app.js exited with code 8
I'm running a postgresql database on heroku, via node. I have my server setup to post to '/submit', which calls a database controller to insert the data into the database. Everything works successfully locally, but when I deploy it to heroku and POST, I get the following error in my heroku logs.
2013-01-21T20:23:43+00:00 heroku[router]: at=info method=POST path=/submit host=[MYDOMAIN].herokuapp.com fwd=[IP] dyno=web.1 queue=0 wait=5ms connect=17ms service=35ms status=200 bytes=2
2013-01-21T20:23:46+00:00 app[web.1]: ^
2013-01-21T20:23:46+00:00 app[web.1]: node.js:201
2013-01-21T20:23:46+00:00 app[web.1]: throw e; // process.nextTick error, or 'error' event on first tick
2013-01-21T20:23:46+00:00 app[web.1]:
2013-01-21T20:23:46+00:00 app[web.1]: at Object.afterConnect [as oncomplete] (net.js:637:18)
2013-01-21T20:23:46+00:00 app[web.1]: Error: connect ECONNREFUSED
2013-01-21T20:23:46+00:00 app[web.1]: at errnoException (net.js:646:11)
2013-01-21T20:23:48+00:00 heroku[web.1]: Process exited with status 1
and a 503 in the application.
Here's the relevant controller code (in coffeescript).
LOCAL_DB = "postgres://localhost:#{DBNAME}"
connect = ->
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client
client.connect()
client
insert = (options) ->
client = connect()
query = client.query "INSERT INTO #{TABLE} VALUES($1, $2, $3, $4);",
[options.uid, options.ls_pref, options.hp_pref, options.date]
query.on "error", onError
query.on "end", -> client.end()
I did promote my database to DATABASE_URL:
$ heroku config | grep DATABASE_URL
> DATABASE_URL: postgres://[URL]
Why is my connection being refused?
It turns out I wasn't passing the database string when creating the client.
# Yes
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client db
client.connect()
# No
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client
client.connect db
The local database was working despite this rather obvious oversight. You've been warned!
I using cloud9 ide coding new project. When I deploy on cloudfoundry from cloud9ide. I have error
Application failed to start. Please note that CloudFoundry uses a different port to listen to. When calling 'listen()' use it like '.listen(process.env.PORT || process.env.VCAP_APP_PORT)'.
This is my source
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
var env = process.env.VCAP_SERVICES ? JSON.parse(process.env.VCAP_SERVICES) : null;
var mongodata = env['mongodb-1.8'][0]['credentials'];
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n' + env);
}).listen(port, host);
This source have error when I get mongo object
var mongodata = env['mongodb-1.8'][0]['credentials'];
But not have this line deploy successful
Please help me !!
Thanks so much
As the error in the cloud9 console probably tells you (as it tells me when i try this :-) ):
haalasdoallalsakdl (CloudFoundry): [5/6] Crash log
============/logs/stderr.log============
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property '0' of undefined
at Object.<anonymous> (/var/vcap/data/dea/apps/haalasdoallalsakdl-0-8be0d413a9ec29a79f665d388ce414bd/app/server.js:7:35)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
So there is no entry in VCAP_SERVICES called like that. When I console.log the process.env variable, there isn't even any service listed.
So you'll have to install the mongodb service for your app. Fastest to do this is via the CF VMC tools (can't run this in cloud9 at the moment, so you'll have to install this locally):
vmc create-service mongodb --bind your_app_name
Then it'll start up fine.
N.B. You can probably fix this in the .yml file, but I don't know how to do this :-)