I am trying to deploy my node.js application (with express and mongoose) to openshift and I am not able to do so. The application works perfectly on my local environment.
my entry point is the file /bin/www
I establish this as the entry point on openshift with this line in the package.json file (per this thread):
"main": "bin/www",
I have made sure to set my mongodb connection using environment variables according to the guide like so:
// default to a localhost configuration:
var mongoConnectionString = 'mongodb://127.0.0.1/code-blog';
// if OPENSHIFT env variables are present, use the available connection info:
if (process.env.OPENSHIFT_MONGODB_DB_PASSWORD) {
mongoConnectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "#" +
process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}
mongoose.connect(mongoConnectionString);
The error that I get is:
remote: Waiting for application port (8080) become available ...
remote: Application 'codeblog' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 558a25bd5973ca7a74000162 (Error activating gear: CLIENT_ERROR: Failed to
execute: 'control start' for /var/lib/openshift/558a25bd5973ca7a74000162/nodejs
remote: #<IO:0x00000000b49380>
remote: #<IO:0x00000000b49308>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed
To ssh://558a25bd5973ca7a74000162#codeblog-donaldsubert.rhcloud.com/~/git/codebl
og.git/
29635a8..7a0e926 master -> master
This is peculior to me because I do not specify port 8080 anywhere. In fact, the default port is specified here:
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
app.set('port', port);
var server = http.createServer(app);
I am not really sure where to go from here. I don't seem to have enough information to determine my next step.
[edit]
I added some logging to test what port this is running on, but the logging statement is never run. Here is the code
console.log("TEST TEST TEST");
var app = require('../app');
var debug = require('debug')('ProjectTemplate:server');
var http = require('http');
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
console.log("PORT: ", port);
and output
TEST TEST TEST
module.js:340
throw err;
^
Error: Cannot find module './routes/logIn'
at Function.Module._resolveFilename (module.js:338:15)
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/558a25bd5973ca7a74000162/app-root/ runtime/repo/app.js:26:13)
TEST TEST TEST is from a logging statement at the beginning of the entry point file. Something seems to fail before it hits the console.log("PORT: ", port); It is probable that this is something to do with app.js where the MongoDb connection is made.
[/edit]
Had exactly same error message: Application 'appname' failed to start (port 8080 not available) on open shift node app
After a lot of reading found out that many different users came to different solutions for the same error message, including myself. So I'd advice not to look for quick solutions for this error.
The most important step is step 1 in the below list.
My solution was to add a missing dependency in package.json, for my particular case I needed to add "bcrypt":"~0.8.5", such a stupid thing!
Now, how did I get to fix the issue only knowing the "port 8080 not available" error:
ssh'd into the app, went to the app repo dir (cd $OPENSHIFT_REPO_DIR) and run npm start
Got [...] Error: Cannot find module 'bcrypt' [...]
Logged out from ssh, run npm info bcrypt | grep "version:", it returned "0.8.5"
Added entry "bcrypt":"~0.8.5" to my package.json and commited/pushed changes.
Problem solved, app runs!
The error turned out to be a generic error that had nothing to do with the port. Apparently, if there is any fatal javascript error, it gives this message.
I have mentioned in detail here ... Application 'appname' failed to start (port 8080 not available) on open shift node app
The solution is you need to specify IP address when listening server.
This is how I have fixed it.
var express = require('express');
var app = express();
var http = require('http');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
server();
});
I don't think that this line will create the server on the specified port
var server = http.createServer(app);
You will need to tell it to listen on the port like so:
server.listen(port);
While your app is listening on the correct port, the thing missing is that you don't specify the IP address to listen on. Add something like the following below app.set('port', port);
app.set('ipaddr', server_ip_address);
For me it was the connection string. The one that was suggested right after you created the mongodb cartridge is not working: Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/'nodejs'
But from this article, by using the OPENSHIFT_MONGODB_DB_URL variable it worked.
if(process.env.OPENSHIFT_MONGODB_DB_URL){
mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + db_name;
}
Related
I'm developing an API on NodeJS using express and I'm getting this error while trying to deploy it to Heroku:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I am using the process.env.PORT variable, as I found here on SO, but it is still not working. Here's the index.js code:
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.json());
var router = require("./router");
app.use('/viatges', router);
app.listen(3000 || process.env.PORT,function(){
console.log("up and running on port "+process.env.PORT);
});
Do you have any idea of what could be causing this issue?
On the heroku logs i see this line
2017-04-01T11:44:07.091181+00:00 app[web.1]: up and running on port 27583
so I assume the PORT enviornment variable is correctly set up...
Nevermind, i just fixed it. I changed the order of the operands in the OR, I assume javascript just went with the first as it was defined.
It now looks like this:
app.listen(process.env.PORT || 3000 ,function(){
console.log("up and running on port "+process.env.PORT);
});
I'm reading about gun db. I want to create an electron app that will sync data between two or more clients and I'm thinking to use heroku to run a gun server. I found this example code of how to setup a gun server using node and express:
var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
var express = require('express');
var Gun = require('..');
require('../axe');
var app = express();
app.use(Gun.serve);
app.use(express.static(__dirname));
var server = app.listen(port);
var gun = Gun({ file: 'data', web: server });
global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun; /// make global to `node --inspect` - debug only
console.log('Server started on port ' + port + ' with /gun');
I don't know if this will work on heroku, as I know, heroku has it's own port to listen to. Also I found an heroku button to quickly deploy a gun server on heroku. If I use it how I can modify the code pushed on heroku if needed? Anyone has experience with this decentralized database?
1. Heroku listens to its own port which you need to bind to. It is already done in the code you provided process.env.PORT.
var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
It binds to port 8765 if the environment variables OPENSHIFT_NODEJS_PORT, VCAP_APP_PORT and PORT doesn't exist and there wasn't an argument provided when executing the program.
2. Heroku's file system are ephemeral. Anything that is not versioned/committed in git is lost. So using Heroku's filesystem as a database is a bad idea.
https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
You need a real database server.
3. Provide the Heroku Deploy Button link
You should be able to just fork the project on GitHub, make your changes there. Heroku Deploy Buttons are reusable.
Note: Everything works perfectly on my localhost environment. When I git push heroku master I get a successful push/deploy. I check the Heroku Logs and I see this error:
[]: Stopping process with SIGKILL
[]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 120 seconds of launch
I have read many posts regarding this issue and the fix that I see is
app.set('port', (process.env.PORT || 4000));
This is not my fix since I am already doing this in my server code. Here is my server code:
var path = require('path');
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 4000));
app.use(express.static(__dirname + '/public'));
app.get('*', function response(req, res) {
res.render(path.join(__dirname, 'public/index.html'));
});
app.listen(app.get('port'), 'localhost', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> Listening on port %s.', app.get('port'));
});
NOTE: In the Heroku logs, ==> Listening on port %s. is printed and THEN the timeout happens after that (with no error). So it does get to the end of my server code without error and it prints the correct, random Heroku port.
Furthermore, webpack -p also creates my bundle.js correctly too.
My package.json has these two commands and they are executed without error:
"start": "NODE_ENV=production webpack -p && node server",
"postinstall": "bower install --force"
I am truly at a loss. Please help!
EDIT: I believe I have truly isolated the problem to my server.js file. I ended up doing webpack -p and pushed up the bundle.js file––essentially bypassing webpack on Heroku. I then do the simple npm start command (which is essentially just node server.js command) and my app still times out when trying to connect to the port.
WOW I figured it out. I had this line in my server:
app.listen(app.get('port'), 'localhost', function onStart(err) ....
I just needed to remove 'localhost' from the listen() function.
I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully
I am trying to deploy a reveal.js application to Heroku. Reveal.js runs on node via grunt connect command. The app also requires ruby for compiling assets on-the-fly. Locally, I can run the app by using grunt serve.
Initially, because of compass being a dependency of grunt watch, Heroku only detected the Gemfile and assumed I was running a ruby app. I used the nodejs custom buildpack to force Heroku to see it as a nodejs app.
Procfile contains
web: grunt serve
Log shows
2013-06-17T13:51:56.187012+00:00 heroku[router]: at=error code=H14 desc="No web processes running"
heroku ps shows nothing either. I can run "heroku run grunt serve" successfully, and I have modified the default Gruntfile.js that comes with reveal to accept process.env i.e.
connect: {
server: {
options: {
port: process.env.PORT || 8000,
base: '.'
}
}
}
As a last attempt, I tried using the Heroku-nodejs-grunt build pack (https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt) which will run a grunt task on deploy to compile assets. Still no luck, heroku logs --tail still shows no process running. Exploring with heroku run reveals that grunt is available, and the grunt serve command successfully executes.
When starting to use the new grunt build pack I got an error with the above Gruntfile.js saying "process" is undefined. I switched the port to 0.
The port on which the webserver will respond. The task will fail if
the specified port is already in use. You can use the special values 0
or '?' to use a system-assigned port.
Didn't work, tried "?", didn't work (still no web process and heroku restart doesn't do anything)
I can't figure out how to get Heroku to use grunt serve as my main web server process!
I was able to make it work using nodejs and expressJs.
By following the heroku "getting started with nodejs", I was able to get a working webapp with expressjs and by declaring this in the web.js:
var express = require("express");
var app = express();
app.use(express.logger());
app.use("/", express.static(__dirname));
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
With this you serve everything from / statically.
You have the sources here: https://github.com/MichaelBitard/revealjs_heroku and a working example here: http://murmuring-cove-4212.herokuapp.com/
Your problem was that by default grunt serve binds to localhost. For it to work you will need to do a couple of small changes to reveal.js:
First add grunt-cli as a devDependency:
diff --git a/package.json b/package.json
index 10489bb..4c58442 100644
--- a/package.json
+++ b/package.json
## -36,6 +36,7 ##
"grunt-contrib-connect": "~0.8.0",
"grunt-autoprefixer": "~1.0.1",
"grunt-zip": "~0.7.0",
+ "grunt-cli": "~0.1.13",
"grunt": "~0.4.0",
"node-sass": "~0.9.3"
},
Then add a hostname parameter to grunt that will be used to bind to 0.0.0.0 instead of localhost.
diff --git a/Gruntfile.js b/Gruntfile.js
index 3e67b9f..b2bfc47 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
## -1,5 +1,6 ##
/* global module:false */
module.exports = function(grunt) {
+ var hostname = grunt.option('hostname') || 'localhost';
var port = grunt.option('port') || 8000;
// Project configuration
grunt.initConfig({
## -94,6 +95,7 ## module.exports = function(grunt) {
connect: {
server: {
options: {
+ hostname: hostname,
port: port,
base: '.',
livereload: true,
Now you can create a Procfile with the following contents to deploy to Heroku:
web: npm install && node_modules/.bin/grunt serve --hostname 0.0.0.0 --port $PORT
I have created a PR for the needed changes to reveal.js.
Currently with express v~4.13.3 express.logger() is deprecated and is not included with the express package. To solve this I had to import the dependency morgan.
My web.js file ended up being the following:
var express = require('express');
var morgan = require('morgan');
var app = express();
var port = process.env.PORT || 5000;
app.use(morgan('combined'));
app.use('/', express.static(__dirname));
app.listen(port, function() {
console.log('Server started on ' + port);
});
As well, I needed to update my package.json to include the morgan lib. My dependencies in the file works with:
...
"dependencies": {
"express": "~4.13.3",
"morgan": "~1.7.0",
"grunt-cli": "~0.1.13",
"mustache": "~2.2.1",
"socket.io": "~1.3.7"
},
...