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

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.

Related

parse-Server cloudCode with nodejs

I am using parse-server and I want to use nodejs with cloudCode as in the example below.
This is the example:
Adding nodejs to Parse
here is the example code from the link
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var ParseCloud = require('parse-cloud-express');
var Parse = ParseCloud.Parse;
var app = express();
// Host static files from public/
app.use(express.static(__dirname + '/public'));
// Define a Cloud Code function:
Parse.Cloud.define('hello', function(req, res) {
res.success('Hello from Cloud Code on Node.');
});
// Mount the Cloud Code routes on the main Express app at /webhooks/
// The cloud function above will be available at /webhooks/function_hello
app.use('/webhooks', ParseCloud.app);
// Launch the HTTP server
var port = process.env.PORT || 80;
var server = http.createServer(app);
server.listen(port, function() {
console.log('Cloud Code on Node running on port ' + port + '.');
});
console.log(process.env.PORT);
I have imported all the required modules, but still, when I run the server and try to go to the link "127.0.0.1/webhooks/function_hello" I get back Cannot GET /webhooks/function_hello
Any advise?
*OUTPUT when i run the script *
undefined
Cloud Code on Node running on port 80.
UPDATE it seems that with parse's shutdown that they have changed support status for cloudcode which affects integrating it with NodeJs
Had the same issue. GET doesn't work here. You need to make a POST request, and then you'll get {"success":"Hello from Cloud Code on Node."}
Please make sure you are running the right script with node SCRIPT_NAME
It appears your express server is set to port 5000.
See: var port = process.env.PORT || 5000;
Change your URL to http://127.0.0.1:5000/webhooks/function_hello or localhost:5000/webhooks/function_hello and it should appear
If you want to run on the default port (80) you will need to run with sudo for your script and make the following change to the code.
var port = process.env.PORT || 80;
Add a folder to your directory named public. Inside that folder place a file named index.html. Type Hello World in that file, save it. Restart your server. See if you can open http://127.0.0.1/.

Neo4j in cloudfoundry.

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);

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).

Why won't my app establish websocket connection on Heroku?

I am trying to deploy my nodejs app on heroku. I cannot get the websocket connection to establish. I have read everything I could find, tried every example and none of them seem to work for me.
I when I try to open my page "heroku open", nodejs is correctly giving me the html file. However, the websocket 'ws' connection never establishes.
My server.js has these configurations:
var pport = process.env.PORT || 5000;
server.listen(pport, function(err) {
if(!err) { console.log("Listening on port " + pport); }
});
Side Note
When I check "heroku logs", I find that the port my app is running on is a random 5 digit number. ( like 28896, 53365, etc.) It never actually runs on the second half || 5000.
But the thing is, in order for my game.html file to establish a websocket connection, it needs to know what port.
I have tried the following client configurations, none have worked:
1)
var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);
2)
var host = location.origin.replace(/^http/, 'ws');
host = host + ":5000";
this.connection = new WebSocket(host);
3)
this.connection = new WebSocket('ws://infinite-earth-7708.herokuapp.com/');
I have also done what their website said, and attempted to use the following after deploying my app:
heroku config:add NODE_ENV=production
Please advise
Well I figured it out. Here is what you should know:
I did not change my server configurations from my original post.
My client configurations looked like this:
var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);
But here is the kicker.
On the terminal I used the following command:
heroku labs:enable websockets
And voila, it works! I hope this helps someone.

ERR_SSL_PROTOCOL_ERROR browser error message while running a https server in nodejs express

I have a number of nodejs applications running on Express using the code below. They all run fine using code similar to the following:
fs = require 'fs'
https = require 'https'
express = require 'express'
server_port = 3000
keys_dir = 'keys/' server_options = {
key : fs.readFileSync(keys_dir + 'privatekey.pem'),
cert : fs.readFileSync(keys_dir + 'certificate.pem') } app = express.createServer(server_options)
app.listen server_port
console.log "HTTPS Server started on port #{server_port}"
However, when trying to create a new application using this code I see a ERR_SSL_PROTOCOL_ERROR when starting the https server. Any idea what is causing this problem?
I discovered that was caused when moving from express 2.5.8 to express 3 - specifically 3.0.0beta4. When creating a new project the version pulled from npm had changed to the version 3 series. Even though the module is marked as "beta" when you run express --version this version is what is installed now when running npm install express. The details of the changes are outlined here.
To solve this for my case I used the following code:
const fs = require("fs");
const https = require("https");
const express = require("express");
const keysDir = "keys/";
const options = {
key : fs.readFileSync(keysDir + "privatekey.pem"),
ca : fs.readFileSync(keysDir + "certrequest.csr"),
cert : fs.readFileSync(keysDir + "certificate.pem")
};
const app = express();
https.createServer(options, app).listen(3000);

Resources