How to deploy node app on openshift and run it? - node.js

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

Related

Getting complete ip addrress in express, node.js

I was trying to get complete IP address of the client using express and node.js but what I am getting is ::1. I tried reading this How to get IP address in node.js express not could not find the solution
Here is my code.
const express = require('express')
const app = express()
const middleware = (req, _, next) => {
console.log(req.ip)
// Here I am getting ::1
next()
}
app.get("/*",middleware, (req, res) => {
res.send("Hello")
})
app.listen(3000,() => console.log("Server started"))
The above code is in the index.js file
Here is my package.json file
{
"name": "one",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Can anyone please tell me how how can I get complete ip address?
::1is the IPv6 equivalent of 127.0.0.1 - the loopback address. That is to be expected if you're connecting to your server from the same computer and using localhost as the hostname.
If you connect to your server from a different computer, you should see an actual client IP address.

App engine nodejs deployment error

I am trying some hands-on on Google Cloud Platform, App Engine in specific.
For the same, I've created a simple nodejs application which just send Hello Wold message in the response.
But I am unable to access the endpoint and getting the below error
below are my files:
aap.yaml
runtime: nodejs
env: flex
index.js
'use strict';
const http = require('http');
const port = 443;
const requestHandler = (request, response) => {
console.log(request.url);
response.end('Hello Node.js Server!');
}
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
package.json
{
"name": "test-pro-for-gcm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"deploy": "gcloud app deploy",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
UPDATE 1
In the GCP log, 16:31:59.000 server is listening on 443
The port that will receive HTTP requests is 8080.
Use the PORT environment variable in your code to make it compatible with the App Engine environment: const port = process.env.PORT || 443;

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.

How can deploy node.js application in openshift

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)

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