How can deploy node.js application in openshift - node.js

I created node.js project, with express and angularjs and mongodb.
How can i deploy it to Openshift
the project structure look like this
My user name for the openshift is admin
password is X5900XJSLW4
db name is : topic
Now these are just fake , but they look like this. thanks
THIS WORK LOCALLY VERY FINE, BUT I GET Service Temporarily Unavailable #
http://topic-aggregator16.rhcloud.com/
error log found her
https://github.com/chihabSD/TopicAggregator/blob/master/images/errorlog.png
I REALLY NEED THIS TO WORK ASAP
config.db.mongo = {};
config.web.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || process.env.WEB_PORT || 8080;
config.web.ip = process.env.OPENSHIFT_NODEJS_IP || process.env.IP;
config.db.mongo.url = process.env.OPENSHIFT_MONGODB_DB_URL || 'mongodb://localhost/topic';

To deploy an existing project into openshift you need to modify your
package.json and server.json files:
package.json
Add the server.js file to the main key
Add a start script in the scripts key
"main": "server.js",
"scripts": {
"start": "node server.js",
}
example
{
"name": "to-do",
"version": "1.0.0",
"description": "Simple todo app",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"node",
"angular"
],
"dependencies" : {
"express" : "~4.7.2",
"mongoose" : "~3.6.2",
"morgan" : "~1.2.2",
"body-parser": "~1.5.2",
"method-override": "~2.1.2"
},
"author": "atefth#gmail.com",
"license": "MIT"
}
server.js
Add dynamic port from the openshift env
Add dynamic IP from the openshift env
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.listen(server_port, server_ip_address, function () {
console.log( "Listening on " + server_ip_address + ", server_port " + server_port );
});
Now you can use the rhc --from-code flag to deploy a project from existing code
rhc app create appName nodejs-0.10 -s --from-code=https://github.com/username/app.git
UPDATE
If you're using mongodb you need to update your server.js file
if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
connection_string = 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;
config.db.mongo.url = connection_string;
}
Hope this helps.

In general, OpenShift Online (V2) will publish connection information to your application using environment variables (https://developers.openshift.com/languages/nodejs/environment-variables.html#listen).
Your nodejs webservice will need to use this information to connect to OpenShift's load-balancers.
For this example code, you would need to change
config.web.port = process.env.PORT || process.env.WEB_PORT || 3000;
config.web.ip = process.env.IP;
to:
config.web.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || process.env.WEB_PORT || 3000;
config.web.ip = process.env.OPENSHIFT_NODEJS_IP || process.env.IP;
For more information, see https://developers.openshift.com/languages/nodejs/environment-variables.html#listen
Npm modules cloud-env or config-multipaas may also be helpful if you prefer to stick with simpler, vendor-neutral application config strings (normalized to PORT and IP)

Related

How to deploy socket io & express server on heroku

I'm trying to deploy socket io + express server on heroku for a chat application but i am facing a trouble while deploying the server .
First this is my server code
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];
server.listen(process.env.PORT || 3000);
console.log('Server running...');
io.sockets.on('connection',function(socket){
connections.push(socket);
console.log('Connected %s sockets connected ', connections.length);
//Disconnect
socket.on('disconnect', function(data){
users.splice(users.indexOf(socket.username),1);
connections.splice(connections.indexOf(socket),1);
console.log('Disconneted : %s sockets connected',connections.length);
});
});
This is my package.json file
{
"name": "",
"version": "1.0.0",
"description": "chat application",
"main": "index.js",
"scripts": {
"start": "node index"
},
"author": "",
"license": "ISC",
"dependencies": {
"socket.io": "*",
"express": "*"
}
}
But I'm getting this error
Cannot GET /
Had the same problem (socket.io with express and react).
You can add to server this line:
app.use(express.static('some path to a static file'));
e.g.
app.use(express.static('client/build'))
works for me assuming in package.json has this line at "scripts":
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
This error has nothing wrong deployed application. Your app still waits for connection on port 3000 (or port from process.env.PORT). App responds "Cannot GET /" because you don't have any routes.
To see how routes can be implemented look at this -> https://expressjs.com/en/starter/hello-world.html
To see how connect using socket.io (client part) look at this -> https://socket.io/get-started/chat/#Integrating-Socket-IO

How to deploy node app on openshift and run it?

Just created a very simple hello world app using node:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
app.listen(server_port, server_ip_address, function () {
console.log( "Listening on " + server_ip_address + ", port " + server_port )
});
and it works as it is expected in my local machine,
put that on github and deployed it on openshift, pod created and server running fine:
but when I browse the route which I could find it in Application>>routes menu it says:
Application is not available
The application is currently not serving requests at this endpoint. It may not have been started or is still starting.
I guess I'm using the latest version of openshift since just created an account.
I'm expecting it to show me the Hello world!
update1:
here is my package.json:
{
"name": "npmtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.2"
}
}
Your application server IP address needs to be 0.0.0.0. So you either specify an environment variable for that, or hard code it.
have you configure main file in package.json
and maybe you need to get port dynamically from the environment variables check below link for openshift sample
https://github.com/openshift/nodejs-ex

Nodejs Heroku Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

I have tried number of "Error R10" Solutions, but it doesn't solve the problem for a simple Hello World like Nodejs app with server.js, package.json, index.html and node_module
Hereby adding server.js, package.json and error log
Currently trying this app on free heroku acount
Is it necessary to upload nodejs app on github to host it on heroku
server.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var port = 3000;
var app = express();
// Set Static Folder
app.use(express.static(__dirname));
// Body Parser MW
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(express.static(__dirname + '/'));
} else {
app.use(express.static(__dirname + '/'));
}
app.get("/", function(req, res){
res.render("index.html");
});
app.listen(port, function(){
console.log("Server started ..!");
});
package.json
{
"name": "es1",
"version": "1.0.0",
"description": "eS",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.16.0",
"express": "^4.14.0"
}
}
Attaching heroku logs:
https://drive.google.com/open?id=0B4XtAe7mRM6UVllTQWh1dmplZk0
Change the value of port as follows:
var port = process.env.PORT || 3000;

Openshift node app error when restarting

I have a node/socket.io chat app hosted on openshift, and while it starts correctly if i ssh into the server and do "node main.js" (where main.js is the server script that starts the chat), I can't start the app on the server by web interface, where it would go on automatically; If i just start the app by ssh, it would stop working as soon as i exit the terminal.
I get this error when starting the app by the web interface:
Starting Node.js application...
Application is already stopped.
Warning! Could not start Node.js application!
Failed to execute: 'control restart' for /var/lib/openshift/57003fbe7628e1491d00011e/nodejs
In case it's relevant, my package.json file is
{
"name": "rainychat",
"version": "1.0.0",
"description": "rainychat, my chat app",
"main": "main.js",
"dependencies": {
"express": "^4.13.4",
"socket.io": "^1.4.5",
"validator": "^5.1.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "JG",
"license": "ISC"
}
And here you can see the files of the app by ftp:
I can't decode what that error means...
My main.js code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/chat.html'); // /home/redadmin/public_html/rainychat.com
console.log('enviado');
});
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.listen(app.get('port'), app.get('ip'), function () {
console.log('Listening on port ' + app.get('port'));
});
//... More code
If you're creating a new Node project, start with npm init to create the package.json file. You can add the --auto option to give it safe defaults.
Remember, the JSON file must be valid JSON, so test it with jsonlint or a tool like an online validator.
Any dependencies your project has should be spelled out in the package file. This is done automatically with things like npm install express --save.

Node.js host on OpenShift keeps "Service Temporarily Unavailable"

I am trying out to host a node.js project on Openshift, here is my package.json:
"scripts": {
"start": "node index.js"
},
"main": "index.js",
"dependencies": {
"express": "^4.13.3",
"formidable": "^1.0.17"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
...
and here is my index.js
var express = require('express');
var app = express();
var http = require('http');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
app.get('/', function(req,res){
res.send("Hello World");
});
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();
});
What am I missing and how can I successfully see the expected "Hello World" message? Thanks!
I don't see anything wrong with your could, and I bet “Service Temporarily Unavailable” means exactly that, in this case. Simply try again later after the scheduled maintenance.
Also, check the scheduled maintenances at http://status.openshift.com/ or #openshift_ops

Resources