How to configure custom host in Restify in Cloud9IDE? - node.js

I am not able to give custom host address as process.env.IP to restify server. It is required since Cloud9 IDE works on process.env.IP & process.env.PORT
var restify = require('restify');
var server = restify.createServer({
certificate: ...,
key: ...,
name: 'MyApp',
});
server.listen(8080); //process.env.PORT will go here instead of 8080

Cloud9 will also accept 0.0.0.0 as the IP and 8080 as the port.

Related

Openshift - port to use on deployment

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!
Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

Nodejs port 8000 ,4000 not working on server

I am using nodejs for rest-api and my app running on 2000,3000 but when i use port 8000 it work on browser but it not working on iphone also i was opened port 8000 from whm.
Try to run your application on 0.0.0.0 ip address, this guarantees it would be listening on all the configured network interfaces
var server = app.listen(8000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});

nodejs websocket server ip localhost --> openshift

I am trying to migrate my nodejs websocket server from localhost to OpenShift. I managed to get the server running in openshift, but now I am having problems trying to connect to it from my Unity 3D client.
Server code:
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
console.log(server_port);
console.log(server_ip_address);
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ host: server_ip_address, port: server_port });
The server outputs an ip 127.13.159.1 and a port 8080, but when I try to connect from Unity client using this ip and port it says "Error: The WebSocket connection has already been closed."
I am using this package in Unity for websockets https://www.assetstore.unity3d.com/en/#!/content/38367
new WebSocket(new Uri("ws://127.13.159.1:8080"));
To connect to your application using websockets you need to use your ws://app-domain.rhcloud.com:8000 or wss://app-domain.rhcloud.com:8443 (for secure websockets)

Node.js socket app on openshift

I'm trying to deploy a simple node.js socket app on OpenShift.
First I tried setting up the listener as:
var server = net.createServer(newSocket); //newSocket is a listener method
var port = 8888;
server.listen(port);
and this causes:
Error: listen EACCES
Then I researched a bit and learned that you need to listen using OPENSHIFT_NODEJS properties and set the listener like this:
var server = net.createServer(newSocket);
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8888;
server.listen(port, ipaddr);
Now the app is started at: 127.6.253.1:8080 - however when I try to telnet it using my OpenShift app url and 8080 I get server timeout.
If you have experience with the similar situation let me know.
The code of the app I'm trying to make it work on OpenShift is at https://github.com/denimf/NodeChat
The internal port for the OpenShift app is 8080, but it is exposed externally on port 80 at the URL specified in your control panel. You can also see the app URL in the console by doing:
echo $OPENSHIFT_APP_DNS
Most of the node.js web hosting services don't support socket listener. I solved my problem by hosting the Node app on a dedicated virtual machine.

Having express.js or node accept an http connection over port 443

I have a REST API built with node that communicates over SSL. The server is built uses express and makes use of vhosts and cors. I have recently added a listener on port 80 as well so I can force HTTPS. As I test, I tried to access http://manage.domain.com:443/ but the request just hangs. Neither listeners seem to accept it. All I want to do is redirect that request to https.
I assume you already know this, but you'll need an https server (duh) to serve the HTTPS content. It doesn't matter what port you run it on; 443 is just the default port for HTTPS. If you want HTTP requests to redirect to HTTPS, you'll need both an http and an https server. Here's an example of how your app file should look:
var http = require('http'),
https = require('https'),
express = require('express')
fs = require('fs');
var domain = 'localhost';
var app = express();
app.get('*', function(req, res){
// redirect to HTTPS
res.redirect('https://' + domain + req.path);
});
http.createServer(app).listen(80, function(){
console.log('HTTP listening on port 80');
});
var appSecure = express();
// configure your app here
var options = {
key: fs.readFileSync('ssl_key.pem'),
cert: fs.readFileSync('ssl_cert.crt'),
};
https.createServer(options, appSecure).listen(443, function(){
console.log('HTTPS listening on port 443');
});
Obviously, you will need your SSL key and certificate to make this work.
As you probably know, most systems require elevated privileges to open a port less than 1025; so if you use port 80 and port 443, you'll have to run the app server with elevated privileges (if you're running on OSX/Linux/BSD, just do sudo node app.js).

Resources