Neo4j in cloudfoundry. - node.js

I am pushing neo4j 2.3.3 application to cloudfoundry.
I have neo4j server and neo4j running cloud, which also runs in my local.
As of the data set is inside the neo4j server.
Neo4j browser is node js application, which start on command "grunt server"
but the browser starts up at http://localhost:9000.
How to make the nodejs application (neo4j browser) listen to 9000. I know there process_env. But how to implement it here.
Neo4j browser has a js file(connect.js) which loads the http protocol , host and port.
I need some guidance, in making modifications here.
I have previously read VCAP_SERVICES into a java code.
1. how to add port 9000 to cloud foundry.
2. how to read port env variable from cloud foundry to jd file.
or
Is there any other way around. ?

This is a node js application.
In local : the host name is 127.0.0.1
port is 9000.
server is running
Neo4j browser is a node js application started using "grunt server"
There are no error in the log.
But I how launch the browser. I get 502 bad gateway error.
Local host :127.0.0.1 then what is the host address for cloud foundry.
I did try 0.0.0.0
https://docs.cloudfoundry.org/buildpacks/node/node-tips.html
--------------
var vcapport = process.env.VCAP_APP_PORT || 3000;
var vcaphost = process.env.VCAP_APP_HOST || '0.0.0.0';
server
.listen(vcapport, vcaphost)
.on('listening', function() {
var address = server.address();
//var hostname = options.hostname || '0.0.0.0';
var hostname = vcaphost;
var targetHostname = 'browser.cfappstpanpz2.ebiz.verizon.com';
var target = options.protocol + "://" + vcaphost +":" + vcapport;
//var target = 'http://browser.cfapps.io/';
grunt.log.writeln('Started connect web server on ' + target);
grunt.config.set('connect.' + taskTarget + '.options.hostname', hostname);
grunt.config.set('connect.' + taskTarget + '.options.port', address.port);
grunt.log.writeln('server.address ' + address);
grunt.log.writeln('address.port ' + address.port);
grunt.log.writeln('hostname ' + hostname);
grunt.log.writeln('vcapport ' + vcapport);
grunt.log.writeln('vcaphost ' + vcaphos);
grunt.event.emit('connect.' + taskTarget + '.listening', hostname, address.port);

Related

how to connect angular app on public ip and a node app on local ip?

I have an angular 4 app which is running on 151.233.x.y:8080 and I have a node app which is running on 192.168.t.z:3000! I want to make a connection between them with an HTTP service. the base URL in my service is http://192.168.t.z and my angular app is running by ng serve --port 8080 --host 192.168.87.19 --public 151.233.58.231 but I cannot connect to my node app successfully! whats the problem?
const _isDev = window.location.port.indexOf('8080') > -1;
const protocol = window.location.protocol;
const host = window.location.host;
const apiURI = _isDev ? 'http://192.168.t.z:3001/' : '';
export const CNF = {
appName : 'Demo',
BASE_URI: protocol + "//" + host + '/',
BASE_API: apiURI,
};
Use CNF.BASE_API when you use get post method in http requset.
Its working only your system.Outside is not working.

GCP Compute Engine Firewall Rules for TCP Server

I have created a GCP compute engine instance with a static external ip address. Machine type: n1-standard-2 (2 vCPUs, 7.5 GB memory). OS is Linux/Debian.
My intention is to create a plain Node.js TCP server on the machine. The code is as follows:
var net = require('net');
var HOST = '0.0.0.0';
var PORT = 110;
net.createServer(function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
sock.write('You said "' + data + '"');
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
The client is:
var net = require('net');
var HOST = '104.197.23.132';
var PORT = 110;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write('I am Chuck Norris!');
});
client.on('data', function(data) {
console.log('DATA: ' + data);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
My firewall rules are as follows:
PLEASE NOTE: I am listening on port 110, and the client is trying to connect to the static external ip address. Itt appears that I am enabling TCP traffic over 110 according to firewall rules. The error I see is
Error: connect ETIMEDOUT 104.197.23.132:110
When I ssh into the instance, and run tcp client, I connect successfully. So the final question is, why can't local tcp client (my computer) connect to compute instance? Is there something wrong with my firewall rules / source filters / IP forwarding?
I just solved this problem.
You have the wrong targets. Go to the edit page and click the select menu of "Targets", and then you can select the first option "Apply to all instance" that is the easiest way.
You need to first add firewall rule according to your host's IP, as internal traffic needs to be received from that particular host (your machine)
Then you should be able to ping to GCP Compute Instance.
You should also be able to telnet at the particular port which you configured in your program.
This should be okay.
Also - the customized rule should be added in the Network Tags of instance, to make the rule associated to that instance, after this the instance uses that particular rule.

Openshift not setting environment variable for node project

I have a simple node project that I am trying to get deployed to Openshift Online 3 but having some ip and port problems.
Reading the documentation I need to get some environment variables from openshift system, seems legit. But Openshift doesn't seem to set the variables OPENSHIFT_NODEJS_PORT and OPENSHIFT_NODEJS_IP.
Using the example below:
var express = require('express');
var app = express();
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
var port = process.env.OPENSHIFT_NODEJS_PORT || 1212
console.log('environment_port: ' + process.env.OPENSHIFT_NODEJS_PORT);
console.log('environment_ip: ' + process.env.OPENSHIFT_NODEJS_IP);
app.listen(port, server_ip_address, function (err) {
console.log('Running on port ' + port + ' ip: ' + server_ip_address);
});
I get the following output from openshift logs:
Why isn't Openshift Online 3 setting the system varaibles?
In OpenShift 3, no environment variables will be set. Your HTTP server process should listen on port 8080 if using one of the S2I builders.

EC2 with socket.io

I have set up an aws micro instance for my node application. I use socket.io as well. I am getting the following error:
GET http://localhost:3000/socket.io/1/?t=1393065240268 net::ERR_CONNECTION_REFUSED
in the console at the moment when the socket connection should be created. Apart from this the node app works. I suspect that the GET should not be towards localhost but towards the address of the server.
Note that on the server side node logs that it served socket.io:
debug - served static content /socket.io.js
Here is a picture of the Security Group of my server:
.
Socket.io setup:
env = process.env.NODE_ENV || 'development',
packageJson = require('../package.json'),
http = require('http'),
express = require('express'),
RedisStore = require('connect-redis')(express),
SessionSockets = require('session.socket.io'),
path = require('path'),
settings = require('./settings'),
expose = require('express-expose')
//Configure server for io and session.socket.io
tmpApp = express(),
tmpServer = http.createServer(tmpApp),
io = require('socket.io').listen(tmpServer),
appCookieParser = express.cookieParser(settings.cookie.secret),
appRedisStore = new RedisStore(),
sessionIO = new SessionSockets(io, appRedisStore, appCookieParser)
global.App = {
app: tmpApp,
server: tmpServer,
port: process.env.PORT || 3000,
sessionIO: sessionIO,
io: io,
start: function() {
var setUp = this.util('setUp'),
socketHandler = require('./socketHandler'),
self = this
setUp.initialize(function(err, waitingGames) {
if (err) {
console.log('error at initializing the application')
process.exit(0)
}
if (!self.started) {
self.started = true
self.server.listen(self.port)
socketHandler()
console.log("Running App Version " + App.version + " on port " + App.port + " in " + App.env + " mode")
}
})
},
...
}
UPDATE
When I changed my port to 80 I get a different error:
XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1393067003774. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ec2-54-214-136-70.us-west-2.compute.amazonaws.com' is therefore not allowed access.
I found the problem. It was on the client side. I was connecting to localhost. It's a stupid error, but during development you don't pay attention to these details and it seemed natural that socket.io should connect to the root from where you serve your content.
Since I'm using EC2 and after each restart I get different DNS address I've sent to the page where I'm initializing the socket.io the correct the req.headers.host (using express-expose).

Non-http TCP connection on Cloudfoundry

I'm a nooby mobile developer trying to take advantage of cloudfoundry's service to run my server to handle some chats and character movements.
I'm using Noobhub to achieve this (TCP connection between server and client using Node.js and Corona SDK's TCP connection API)
So basically I'm trying a non-http TCP connection between Cloudfoundry(Node.js) and my machine(lua).
Link to Noobhub(There is a github repo with server AND client side implementation.
I am doing
Client
...
socket.connect("myappname.cloudfoundry.com", 45234)
...
(45234 is from server's process.env.VCAP_APP_PORT value I retrieved from console output I got through "vmc logs myappname" after running the application.)
Server
...
server.listen(process.env.VCAP_APP_PORT)
When I try to connect, it just times out.
On my local machine, doing
Client
...
socket.connect("localhost",8989)
Server
...
server.listen(8989)
works as expected. It's just on cloudfoundry that it doesn't work.
I tried a bunch of other ways of doing this such as setting the client's port connection to 80 and a bunch of others. I saw a few resources but none of them solved it.
I usually blow at asking questions so if you need more information, please ask me!
P.S.
Before you throw this link at me with an angry face D:< , here's a question that shows a similar problem that another person posted.
cannot connect to TCP server on CloudFoundry (localhost node.js works fine)
From here, I can see that this guy was trying to do a similar thing I was doing.
Does the selected answer mean that I MUST use host header (i.e. use http protocol) to connect? Does that also mean cloudfoundry will not support a "TRUE" TCP socket much like heroku or app fog?
Actually, process.env.VCAP_APP_PORT environment variable provides you the port, to which your HTTP traffic is redirected by the Cloud Foundry L7 router (nginx) based on the your apps route (e.g. nodejsapp.vcap.me:80 is redirected to the process.env.VCAP_APP_PORT port on the virtual machine), so you definitely should not use it for the TCP connection. This port should be used to listen HTTP traffic. That is why you example do work locally and do not work on Cloud Foundry.
The approach that worked for me is to listen to the port provided by CF with an HTTP server and then attach Websocket server (websocket.io in my example below) to it. I've created sample echo server that works both locally and in the CF. The content of my Node.js file named example.js is
var host = process.env.VCAP_APP_HOST || "localhost";
var port = process.env.VCAP_APP_PORT || 1245;
var webServerApp = require("http").createServer(webServerHandler);
var websocket = require("websocket.io");
var http = webServerApp.listen(port, host);
var webSocketServer = websocket.attach(http);
function webServerHandler (req, res) {
res.writeHead(200);
res.end("Node.js websockets.");
}
console.log("Web server running at " + host + ":" + port);
//Web Socket part
webSocketServer.on("connection", function (socket) {
console.log("Connection established.");
socket.send("Hi from webSocketServer on connect");
socket.on("message", function (message) {
console.log("Message to echo: " + message);
//Echo back
socket.send(message);
});
socket.on("error", function(error){
console.log("Error: " + error);
});
socket.on("close", function () { console.log("Connection closed."); });
});
The dependency lib websocket.io could be installed running npm install websocket.io command in the same directory. Also there is a manifest.yml file which describes CF deploy arguments:
---
applications:
- name: websocket
command: node example.js
memory: 128M
instances: 1
host: websocket
domain: vcap.me
path: .
So, running cf push from this directory deployed app to my local CFv2 instance (set up with the help of cf_nise_installer)
To test this echo websocket server, I used simple index.html file, which connects to server and sends messages (everything is logged into the console):
<!DOCTYPE html>
<head>
<script>
var socket = null;
var pingData = 1;
var prefix = "ws://";
function connect(){
socket = new WebSocket(prefix + document.getElementById("websocket_url").value);
socket.onopen = function() {
console.log("Connection established");
};
socket.onclose = function(event) {
if (event.wasClean) {
console.log("Connection closed clean");
} else {
console.log("Connection aborted (e.g. server process killed)");
}
console.log("Code: " + event.code + " reason: " + event.reason);
};
socket.onmessage = function(event) {
console.log("Data received: " + event.data);
};
socket.onerror = function(error) {
console.log("Error: " + error.message);
};
}
function ping(){
if( !socket || (socket.readyState != WebSocket.OPEN)){
console.log("Websocket connection not establihed");
return;
}
socket.send(pingData++);
}
</script>
</head>
<body>
ws://<input id="websocket_url">
<button onclick="connect()">connect</button>
<button onclick="ping()">ping</button>
</body>
</html>
Only thing to do left is to enter server address into the textbox of the Index page (websocket.vcap.me in my case), press Connect button and we have working Websocket connection over TCP which could be tested by sending Ping and receiving echo. That worked well in Chrome, however there were some issues with IE 10 and Firefox.
What about "TRUE" TCP socket, there is no exact info: according to the last paragraph here you cannot use any port except 80 and 443 (HTTP and HTTPS) to communicate with your app from outside of Cloud Foundry, which makes me think TCP socket cannot be implemented. However, according to this answer, you can actually use any other port... It seems that some deep investigation on this question is required...
"Cloud Foundry uses an L7 router (ngnix) between clients and apps. The router needs to parse HTTP before it can route requests to apps. This approach does not work for non-HTTP protocols like WebSockets. Folks running node.js are going to run into this issue but there are no easy fixes in the current architecture of Cloud Foundry."
- http://www.subbu.org/blog/2012/03/my-gripes-with-cloud-foundry
I decided to go with pubnub for all my messaging needs.

Resources