Exressjs: how to get https server address and port? - node.js

How can I get https server address and port with expressjs?
With http:
var server = app.listen(3000, "127.0.0.1", function () {
var host = server.address().address;
var port = server.address().port;
console.log(server.address());
console.log('Example app listening at http://%s:%s', host, port);
});
result:
{ address: '127.0.0.1', family: 'IPv4', port: 3000 } Example app
listening at http://127.0.0.1:3000
But with https:
https.createServer(options, app).listen(3000, "127.0.0.1", function () {
console.log('Started!');
var host = https.address;
var port = https.port;
console.log('Example app listening at http://%s:%s', host, port);
});
result:
Example app listening at http://undefined:undefined
Any ideas?

may be this link is helpful to you
https://docs.nodejitsu.com/articles/HTTP/servers/how-to-create-a-HTTPS-server/

Related

How to troubleshoot problem with setting WebRTC server on vps?

I wrote a simple node express server for webRTC using peerjs-server and simple client using peerjs. Everything works fine on localhost, but when I try it on vps, I get error:
Firefox can't connect with server ws://my.vps/peerjs/peerjs?key=peerjs&id=hj3hpekwaa38fr00&token=ymtfvhagiw
PeerJS: Socket closed.
PeerJS: ERROR Error: Lost connection to server.
Error: "Lost connection to server."
emitError https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:16426
_initializeServerConnection https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:12260
emit https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:25516
onclose https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:19350
Server:
const express = require('express');
enter code here`const app = express();
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(express.static('./public'));
const server = app.listen(80, () => { // 3000 on localhost
console.log('Express server listen on port ' + 80);
});
const options = { debug: true };
const peerserver = ExpressPeerServer(server, options);
app.use('/peerjs', peerserver);
app.use('/*', express.static('./public/index.html'));
Client:
var peer = new Peer('', {
host: location.hostname,
port: location.port || (location.protocol === 'https:' ? 443 : 80),
path: '/peerjs',
debug: 3
});
peer.on('open', function (id) {
console.log(id);
});
Any help appreciate.
It looks like you are connecting with server ws://my.vps/, which is a web socket to a server at http://my.vps/ which doesn't seem to exist.
It should probably also be using https (or wss)

Nodejs doesn't redirect to https

I have a node server with express on top. Godaddy is the domain registrar that I use.
If I try to connect with www.example.com doesn't work, only with https://www.example.com.
This is how the code looks like:
app.use(express_enforces_ssl());
app.enable('trust proxy');
app.use (function (req, res, next) {
if(req.secure){
next();
}else {
res.redirect('https://' + req.headers.host + req.url);
}
});
var options = {
ca: fs.readFileSync('./gd_bundle-g2-g1.crt'),
key: fs.readFileSync('./server_cert_key.key'),
cert: fs.readFileSync('./cfbe0f99da37dcae.crt')
};
https = require('https').createServer(options, app);
io = require('socket.io')(https);
However, if I 'ping www.example.com' the server is reached, but not in the browser.
The process is running on port 443 (dedicated port for secured connection).
I have made port forwarding from internal port 443 with output on port 80, this is how domain registrar works, only with port 80.
I don't know how can I fix it. Do you have any idea ?
DNS doesn't usually require ports info. Browser automatically assumes 80 for http scheme and 443 for https scheme.
The app code is only listening on 443. You need to add additional:
require('http').createServer(app).listen(80);
Try normalizing the port before listen to it.
In the code below, the port will be normalized according to the app settings. If none were found, it will be defaulted to port 80
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) return val;
if (port >= 0) return port;
return false;
}
//port 443 if https:// is detected
var port = normalizePort(process.env.PORT || '80');
// for me, I imported ('http') instead of ('https')
// but it also works on port 443
https.listen(port, function listening() {
console.log('Listening on %d', server.address().port);
});

Https server with socket and express

I'm using SSL to encrypt my backend but my current solution opens two ports, one for sockets and the other for express, any approach to start both on the same port like HTTP ?
const port=4000;
if(process.env.ENABLE_SSL=='true')
{
////two ports are open 8989,4000
server = https.createServer({
key: fs.readFileSync("sslLocation/ssl.key"),
cert: fs.readFileSync("sslLocation/ssl.cert")
},app).listen("8989", '0.0.0.0',function(){
console.log('Express server listening to port 8989');
});
global.io = require('socket.io').listen(server);
app.listen(port);
}
else
{
////one port only
// start the server
server = app.listen(port, function () {
console.log(`App is running at: localhost:${server.address().port}`);
});
global.io = require('socket.io').listen(server);
}
also running app.listen(server) in the ssl section, i can't access the apis

Get IPv4 Address of client in socket.io

When I log the 'socket' object during socket.on('connection'), I get the following information:
_peername: { address: '::1', family: 'IPv6', port: 57535 }
How do I change the family to IPv4 during socket connection?
I was testing around and found that on the client if I use
var socket = io('http://localhost:8000', { transports: ['websocket']});
the socket.handshake.address returns ::1
If I use
var socket = io('http://server_ip:8000', { transports: ['websocket']});
the socket.handshake.address return ::ffff:ipv4address
When you create an http instance, make sure you bind your connection to an IPv4 address.
Example:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
http.listen(8000, '0.0.0.0', function(){
console.log('listening on *:8000');
});
That should do the trick. :)
This should listen on IPv4; Try it and let me know...
var net = require('net');
var server = net.createServer();
server.on('connection', handleConnection);
server.listen({
host: 'localhost',
port: 8000,
exclusive: true
}, function() {
console.log('server listening to %j', server.address());
});
Output:
server listening to {"address":"127.0.0.1","family":"IPv4","port":8000}

Openshift port for node.js

I have a server that won't start correctly on openshift. This is my code:
var connect = require("connect");
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var httpServer = connect.createServer(connect.static(__dirname + "/public")).listen(port);
console.log("Listening on " + port + "...");
I keep getting this error:
info: socket.io started
warn: error raised: Error: listen EACCES
DEBUG: Program node server.js exited with code 0
DEBUG: Starting child process with 'node server.js'
Listening on 8080...
How can I solve this?
You need to bind the listening IP address to process.env.OPENSHIFT_NODEJS_IP. Here is the example from my working code (I'm using Express) on OpenShift.
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
app.listen(port, ipaddress, function() {
// Do your stuff
});
We need to specify the binding to your OPENSHIFT_NODEJS_IP in your listen like:
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1"; (do not forget quotes)
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

Resources