Setup peerJS server combined with express - node.js

I'm trying to setup my own peerJS server following the readme on https://github.com/peers/peerjs-server#combining-with-existing-express-app
my code on server
port = process.env.PORT or 8080
http = require 'http'
express = require 'express'
app = express()
server = http.createServer app
app.use '/peerjs', ExpressPeerServer server, debug : on
server.listen port
server.listen 9000
my code on client
peer = new Peer
host : 'localhost'
port : 9000
secure : no
config :
iceServers : [ url : 'stun:stun.l.google.com:19302' ]
I get this error on client console
GET http://localhost:9000/peerjs/peerjs/id?ts=14150106969530.4679094860330224 net::ERR_CONNECTION_REFUSED

In case you are still looking for a way to establish your own peer server, then here is what I have done to make it work for me.
server.js
// initialize express
var express = require('express');
var app = express();
// create express peer server
var ExpressPeerServer = require('peer').ExpressPeerServer;
var options = {
debug: true
}
// create a http server instance to listen to request
var server = require('http').createServer(app);
// peerjs is the path that the peerjs server will be connected to.
app.use('/peerjs', ExpressPeerServer(server, options));
// Now listen to your ip and port.
server.listen(8878, "192.168.1.14");
Client side code
I guess, you should not have much problem in this, but if you are wondering what to put for certain parameters, then here is the initialization of the peer object:
var peer = new Peer({
host: '192.168.1.14',
port: 8878,
path: '/peerjs',
config: { 'iceServers': [
{ url: 'stun:stun01.sipphone.com' },
{ url: 'stun:stun.ekiga.net' },
{ url: 'stun:stunserver.org' },
{ url: 'stun:stun.softjoys.com' },
{ url: 'stun:stun.voiparound.com' },
{ url: 'stun:stun.voipbuster.com' },
{ url: 'stun:stun.voipstunt.com' },
{ url: 'stun:stun.voxgratia.org' },
{ url: 'stun:stun.xten.com' },
{
url: 'turn:192.158.29.39:3478?transport=udp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
},
{
url: 'turn:192.158.29.39:3478?transport=tcp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
}
]
},
debug: 3
});
This should help you to establish the connection.

One have to set a path to the server on their client side like this:
var peer = new Peer('your_peer_id', {host: 'localhost', port: 9000, path: '/peerjs'});
(this path has to be the same with the routing path to your ExpressPeerServer)

Related

Unable to connect to socket.io in production environment

In development, this is how I was connecting to socket.io:
// server.js
const express = require('express')
const app = express()
const port = 8000
const server = app.listen(port, () =>
console.log(`Server is running on port ${port}`)
);
const io = require('socket.io')(server, {
path: '/socket.io',
pingTimeout: 60000,
cors: {
origin: 'http://localhost:3000',
methods: ['GET', 'POST'],
allowedHeaders: ['Content-type'],
},
maxHttpBufferSize: 1e8,
});
// App.js
import io from 'socket.io-client';
let socket;
const App = () => {
useEffect(() => {
socket = io(
'http://localhost:8000/',
{ path: '/socket.io' },
{ reconnection: true }
);
}
})
It made sense that in production I should just need to change instances of localhost to my domain, however doing so gave me this error:
net::ERR_FAILED 200 (OK)
From research I learned this was a CORS error, so I then amended my domain in App.js to include the port :8000 but then that brings up this error:
net::ERR_CONNECTION_TIMED_OUT
My site in production is running on https and I have amended my domain in both instances accordingly. I have also enabled the uncomplicated firewall through my host. Hoping someone out there can help me to understand what the issue is.

Login failed for user using tedious

I am using tedious to connect to SQL Server Express, but I get a login failed error.
Server browser and SQL Server service are running, TCP/IP is enabled in network protocol. User name is enabled moreover I am able to login with .NET code but node.js code is really not working for me
var express = require('express'),
app = express(),
port = process.env.PORT || 3000;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var Tedious = require('tedious');
var config = {
userName: 'sa',
password: 'password',
server: 'localhost',
options: {
encrypt: true,
database: 'MediWorks',
instanceName: 'SQLEXPRESS01'
},
port : '1433'
};
var connection = new Tedious.Connection(config);
connection.on('connect', function(err) {
if (err)
{
console.log(err)
}
else
{
console.log("Connected");
}
});
Just for your information, tedious has deprecated the way you use config. A working example is like the following:
var config = {
server: "localhost",
authentication: {
type: "default",
options: {
userName: "yourusername",
password: "yourpassword"
}
},
};
tedious Github issue that mentions this.

NodeJS Subdomain w/ vhost and greenlock-express

I'm new to Node and I want my website, dacio.app, working with subdomains for my college projects using vhost.
However, I need to have it secured due to the requirement for .app domains, so I'm using greenlock-express to automate it.
Don't be frontin', yo! TLS SNI 'giphy.dacio.app' does not match 'Host:
potatoes.dacio.app'
I've tried using the vhost example in the repo, but it doesn't look like server-static supports express apps.
Any tips on how to get this working? I keep hearing about reverse proxies, but I'm not sure if it's worth the effort as I don't even know if it would work - would it help?
server.js
#!/usr/bin/env node
'use strict';
// DEPENDENCIES
const express = require('express');
const vhost = require('vhost');
const path = require('path');
const glx = require('greenlock-express');
// MIDDLEWARE
const app = express();
const giphyApp = require('../giphy-search');
const potatoesApp = require('../rotten-potatoes');
const portfolioApp = require('../dacio.app');
// ROUTES
app.use(vhost('giphy.dacio.app', giphyApp));
app.use(vhost('potatoes.dacio.app', potatoesApp));
app.use(portfolioApp);
// GREENLOCK for HTTPS
glx.create({
version: 'draft-11',
server: 'https://acme-v02.api.letsencrypt.org/directory',
email: 'dacioromero#gmail.com',
agreeTos: true,
approveDomains: [ 'dacio.app', 'giphy.dacio.app', 'potatoes.dacio.app' ],
configDir: '~/.config/acme/',
app: app,
communityMember: false
}).listen(80, 443);
I've switched to using redbird which seems to accomplish everything I was hoping to do.
const path = require('path')
const proxy = require('redbird')({
port: 80,
letsencrypt: {
path: path.join(__dirname, '/certs'),
port: 9999
},
ssl: {
http2: true,
port: 443
}
});
proxy.register('dacio.app', 'http://localhost:8080', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true,
}
}
});
proxy.register('giphy.dacio.app', 'http://localhost:8081', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true
}
}
})
proxy.register('potatoes.dacio.app', 'http://localhost:8082', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true
}
}
});

Redirect http to https in Hapi.js

I do have the following configuration for my hapi server
const server = new Hapi.Server();
const tls = {
cert: fs.readFileSync(path.join(__dirname, '../certificates/cert.crt')),
key: fs.readFileSync(path.join(__dirname, '../certificates/cert.key')),
};
server.connection({
port: process.env.PORT_HTTP || 80,
host: process.env.HOST || 'localhost',
});
server.connection({
port: process.env.PORT_HTTPS || 443,
host: process.env.HOST || 'localhost',
tls,
});
The server is working ok on both, http and https, but I would like to redirect all the traffic from the http to https.
How should I proceed, tried already to register the hapi-require-https npm module but the traffic still remain the same, nothing happens.
Create an extra server for http requests and bind them to redirect function.
var Hapi = require('hapi');
var http = new Hapi.Server(80);
var server = new Hapi.Server(443, { tls: {} });
var redirect = function () {
this.reply.redirect('https://your.site/' + this.params.path);
});
http.route({ method: '*', path: '/{path*}', handler: redirect });
Update(other option)
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
if(request.headers.referer.split(':')[0] == "http"){
this.reply.redirect('https://your.site' + this.params.path);
}
}
});
How about this? Binding them both
var http = new Hapi.Server(80); // our extra server
http.route({
method: '*',
path: '/{path*}',
handler:
function (request, reply) {
// if(request.headers.referer.split(':')[0] == "http"){
this.reply.redirect('https://your.site' + this.params.path);
// }
}
});
Create two server instances to handle http & https traffic seperately.
var Hapi = require('hapi');
var server = new Hapi.Server(80);
var httpsServer = new Hapi.Server(443, { tls: { // your certificates here} });
Now register the hapi-gate plugin to the base server so that it redirects the traffic to https.
server.register({
register: require('hapi-gate'),
options: {https: true} // will force https on all requests
});
You can also use the hapi-require-https plugin instead.

heroku eureka-js-client node- Spring Cloud Netflix Support -

What I want to achieve is:
registering a node.js service in my eureka-server in heroku.
So far i can register a regular eureka-client in my eureka-server in heroku, without problems. But i am getting really confused with the configuration when try with node.js service...
I thought eureka-js-client was the solution, no success so far...
Code below.
const express = require('express');
const path = require('path');
const port = process.env.PORT || 3090;
const app = express();
app.use(express.static(__dirname));
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'))
});
const Eureka = require('eureka-js-client').Eureka;
const eureka = new Eureka({
instance: {
app: 'sheila',
hostName: 'localhost',
ipAddr: '127.0.0.1',
statusPageUrl: 'http://localhost:5000',
healthCheckUrl: 'http://localhost:5000/health',
port: {
'$': 5000,
'#enabled': 'true',
},
vipAddress: 'myvip',
dataCenterInfo: {
'#Class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
eureka: {
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps/',
},
});
eureka.logger.level('debug');
eureka.start(function(error){
console.log(error || 'complete');
});
// ------------------ Server Config --------------------------------------------
var server = app.listen(5000, function () {
var port = server.address().port;
console.log('Listening at %s', port);
});
First i've tried locally after running docker run -it -p 8761:8761 springcloud/eureka on my docker console. But i get this error:
Problem making eureka request { [Error: connect ECONNREFUSED 127.0.0.1:8761]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8761 }
if i run it as it is from a heroku service, It does not execute :( :(
I also tried by substituting the host for the url of my heroku eureka server, but then i get 401 or 404 errors. The eureka server requires a password which i added in my heroku client js .
Any ideas?
You need to include an instanceId which should be a unique identifier of this instance.
const eureka = new Eureka({
instance: {
instanceId:'sheila',
app: 'sheila',
hostName: 'localhost',
ipAddr: '127.0.0.1',
statusPageUrl: 'http://localhost:5000',
healthCheckUrl: 'http://localhost:5000/health',
port: {
'$': 5000,
'#enabled': 'true',
},
vipAddress: 'myvip',
dataCenterInfo: {
'#Class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
eureka: {
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps/',
},
});

Resources