I am trying to get an express peerJs server up and running locally over HTTPS. Now if i start the peerJS server via the CLI then i can connect to it just fine and everything works as it should, but once i try to run the peerJS server via my script, i cant connect to it. I get a net::ERR_CONNECTION_REFUSED error in the console.
Working code:
--server.js
const fs = require('fs');
const express = require('express');
const app = express();
const server = require('https').Server(
{ key: fs.readFileSync('vuekey.pem'), cert: fs.readFileSync('vuecert.pem') },
app
);
const io = require('socket.io')(server);
const { v4: uuidV4 } = require('uuid');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.redirect(`/${uuidV4()}`);
});
app.get('/:room', (req, res) => {
res.render('room', { roomId: req.params.room });
});
io.on('connection', (socket) => {
socket.on('join-room', (roomId, userId) => {
socket.join(roomId);
socket.to(roomId).broadcast.emit('user-connected', userId);
socket.on('disconnect', () => {
socket.to(roomId).broadcast.emit('user-disconnected', userId);
});
});
});
server.listen(3000);
--client.js
const myPeer = new Peer(undefined, {
host: '/',
port: '3001',
});
I first run peerjs --port 3001 --sslkey 'E:\myPath\vuekey.pem' --sslcert 'E:\myPath\vuecert.pem' to manually start the PeerJS server. This all works fine and i have no issues. But i want to have the PeerJS server run as part of my main server script. So im doing that with ExpressPeerServer. So in my server.js file i add the below code, but thats when the console errors get thrown.
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
debug: true,
port: '3001',
ssl: {
key: fs.readFileSync('vuekey.pem'),
cert: fs.readFileSync('vuecert.pem'),
},
});
app.use('/peerjs', peerServer);
What is the PeerJS CLI doing differently than my server.js code? How can i resolve this?
I was able to figure it out finally. The express server and the peer server were in separate ports and i needed to add the path parameter in the Peer object.
--server.js
const peerServer = ExpressPeerServer(server, {
debug: true,
port: '3000',
ssl: {
key: fs.readFileSync('vuekey.pem'),
cert: fs.readFileSync('vuecert.pem'),
},
});
--client.js
const myPeer = new Peer(undefined, {
host: '/', // This will force the localhost/IP of the machine
path: '/peerjs', // Path that was specified in the server.js file for the peerServer's app.use() method.
port: '3000', // This needs to be the same port as specified in the server.js
});
After making these minor changes, everything works as expected over HTTPS
Related
I receive the following error when using my browser to make a GET request for all entries in a database table:
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
Error in GET /products
The relevant folder structure looks like:
server/
db/
index.js
routes/
products.js
index.js
The files involved are:
server/index.js
const express = require("express");
const cors = require("cors");
const app = express();
const PORT = process.env.port || 3001;
app.use(cors());
app.use(express.json());
// const usersRouter = require('./routes/users')
// app.use('/users', usersRouter)
// const authRouter = require('./routes/auth')
// app.use(['/login', '/register'], authRouter)
const productsRouter = require('./routes/products')
app.use('/products', productsRouter)
// const cartsRouter = require('./routes/carts')
// app.use('/carts', cartsRouter)
// const ordersRouter = require('./routes/orders')
// app.use('/orders', ordersRouter)
app.listen(PORT, () => {
console.log(`Listening on ${PORT}`);
});
Initializes the server on port 3001 and creates productsRouter.
server/db/index.js
const { Pool, Client } = require("pg");
const pool = new Pool({
user: "postgres",
host: "localhost",
database: "ecommerce",
password: "postgres",
port: "5432",
});
module.exports = {
query: (text, params, callback) => {
return pool.query(text, params, callback)
},
}
Provides the function for making queries.
server/routes/products.js
const express = require("express");
const products = express.Router();
const db = require("../db/index");
products.get("/", async (req, res) => {
try {
const products = await db.query("SELECT * FROM products");
return res.status(201).send(products.rows);
} catch (err) {
console.log(err.stack);
console.log("Error in GET /products");
res.status(500).send("Server Error Occurred");
}
});
Provides Node.js logic for making requests using db.query calls.
Steps to produce error:
Launch/connect Postbird on port 5432 on localhost.
Run ‘node index.js’ on server/index.js to start Node.js server
listening on port 3001.
Navigate browser to localhost:3001/products to initiate GET request.
Self-taught programmer here. So it turns out that when Codecademy told me to install Windows Subsystem for Linux (WSL), I didn't realize it was truly, essentially a mostly separate system from my main OS. I was using WSL's terminal in VS Code and when I ran 'node index.js', index.js was running on WSL. Therefore, it was unaware of the perfectly good, perfectly accessible instance of Postgresql running on port 5432 of my main OS.
Basically, it was refusing to connect to port 5432 on WSL because there wasn't any Postgres instance listening on that port of WSL; in fact, it wasn't even installed on WSL.
I switched to my Bash terminal, ran 'node index.js', navigated my browser to the URL that calls the GET request which returns the results of a SELECT on all tables in my database, and voila! It worked.
I'm trying to build a simple peer to peer image sharing demo app using Node.JS and PeerJS.
But when I try to run the app, I get an error ReferenceError: navigator is not defined.
This is a node.js backend app so I don't get why PeerJS is requesting to identify the navigator.
Could you please help me spot the problem?
Thanks in advance!
app.js
const express = require('express');
const fs = require('fs');
const { resolve } = require('path');
const Peer = require('peerjs');
const app = express();
const port = 9500;
const sender = new Peer('sender', {
host: 'localhost',
port: port,
path: '/'
});
const receiver = new Peer('receiver', {
host: 'localhost',
port: port,
path: '/'
});
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.get('/send', (req, res) => {
...
...
let image = readDataset();
const conn = sender.connect('receiver');
conn.on('open',()=> {
conn.send({
photo: image,
});
res.status(200).send("Image sent.");
});
});
app.get('/receive', (req, res) => {
receiver.on('connection', conn => {
conn.on('data', data => {
...
...
res.status(200).send("Image received and saved.");
});
});
});
// set the app to listen on the port
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
I am very new to learning and using the MERN stack. Currently, I am having a problem where when I try to run "nodemon server", the port opens on instead of despite specifying in server.js that I want it to start on 5000. I am having trouble sending POST requests to using Insomnia with the current server.js code below (I get a 404 error) and think that this might be causing the POST command to fail. Moreover, it also doesn't run console.log('Server is running on port: ${port}'); into the terminal I am running the server from. Any feedback is appreciated.
server.js
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB database connection established successfully");
});
const courseRouter = require("./routes/course");
const termRouter = require("./routes/term");
const userRouter = require("./routes/user");
app.use("/course", courseRouter);
app.use("/term", termRouter);
app.use("/user", userRouter);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
I'm working to setup a Node backend to feed data and communicate with ReactJS for my frontend. Ultimately I am developing new company software to replace our current Transportation System.
I utilize Amazon EC2 Ubuntu 16.04 - for my own reasons for my business - and I simply cannot get my ReactJS frontend with Socket.IO to communicate with my nodeJS backend with Socket.IO on http://localhost:4000/.
This is my App.js in my react frontend when it calls
import React, { Component } from 'react';
import logo from './logo.svg';
import ioClient from 'socket.io-client';
import './App.css';
var socket;
class App extends Component {
constructor() {
super();
this.state = {
endpoint: 'http://localhost:4000/'
};
socket = ioClient(this.state.endpoint);
}
This is my nodeJS index for the backend
const mysql = require('mysql');
const http = require('http');
const express = require('express');
const cors = require('cors');
const app = express();
const server = http.createServer(app);
const io = require('socket.io').listen(server);
app.use(cors());
app.get('/', (req, res) => {
res.send('Server running on port 4000')
});
const sqlCon = mysql.createConnection({
host: 'localhost',
user: 'admin-user',
password: 'admin-pass',
database: 'sample'
});
sqlCon.connect( (err) => {
if (err) throw err;
console.log('Connected!');
});
io.on('connection', (socket) => {
console.log('user connected');
});
server.listen(4000, "localhost", () => {
console.log('Node Server Running on 4000')
});
I can get it to communicate via my actual Public IP address, but not via localhost. I really don't want to expose my backend on my public IP address to communicate with it for all users. This has probably been asked before, but I honestly can't find a clear answer for it anywhere and I've been looking for 3 days now. Node has no problem executing, and like I said if I create the socket.io connection from the public IP, I can get it to communicate and as far as I can tell node has no problem running the rest of the script as it connects to mariaDB no problem.
This is the error I keep receiving in my Chrome console.
polling-xhr.js:271 GET http://localhost:4000/socket.io/?EIO=3&transport=polling&t=MvBS0bE net::ERR_CONNECTION_REFUSED
polling-xhr.js:271 GET http://localhost:4000/socket.io/?EIO=3&transport=polling&t=MvBS3H8 net::ERR_CONNECTION_REFUSED
I'm running React via npm start for the time being, so my localhost:3000 is being reverse proxied to nginx for my React frontend to be visible on my public EC2 IP via port 80.
Any help is appreciated!
It may be a cross origin request issue. Have you tried to enable CORS on your app. You can also use proxy in your react app package.json if you do not want to enable cors on your app.
In your react app package.json you can add
"proxy":"http://localhost:4000"
It's probably because the port you are using isn't available in the server-side when it's running.
Use the server-side port like this,
var port = process.env.PORT || 3000;
server.listen(port, "localhost", () => {
console.log('Node Server Running on 4000')
});
and on the client-side just connect to the app URL, like,
this.state = {
endpoint: '/'
};
socket = ioClient(this.state.endpoint);
Just clean up your server a bit. Take this guy run him from whatever terminal or ide you use to get your server going.
let startTime = Date.now();
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
var cors = require('cors');
const app = express();
app.use(compression());
app.use(bodyParser.json({ limit: '32mb' }));
app.use(bodyParser.urlencoded({ limit: '32mb', extended: false }));
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(cors({ origin: 'null' }));
const request = require('request');
const port = 4000;
let pm2InstanceNumber = parseInt(process.env.NODE_APP_INSTANCE) || 0;
http.listen(port + pm2InstanceNumber, err => {
if (err) {
console.error(err);
}
console.log('Listening http://localhost:%d in %s mode', port + pm2InstanceNumber);
console.log('Time to server start: ' + (Date.now() - startTime) / 1000.0 + ' seconds');
setTimeout(() => {
try {
process.send('ready');
} catch (e) {}
}, 2000);
app.get('/', (req, res) => {
res.send('Server running on port 4000')
});
});
or just run node filename.js to serve this guy up.
I have server side rendering react app in which i have proxied all http calls to different port. Please see the code below for http proxy.
import proxy from "express-http-proxy";
const app = express();
const bodyParser = require("body-parser");
const http = require('http');
const httpProxy = require('http-proxy');
const PORT = process.env.PORT || 3001;
httpProxy.createServer({
target: 'ws://localhost:4000',
ws: true
}).listen(PORT); //Throws error since 3000 port is already used by //app.listen.
app.use(
"/api",
proxy("http://localhost:4000/", {
proxyReqOptDecorator(opts) {
opts.headers["x-forwarded-host"] = "http://localhost:4000/";
return opts;
}
})
);
app.post("/logger", (req, res) => {
logger.debug(req.body.data);
res.send({ status: "SUCCESS" });
});
app.listen(PORT, () => {
logger.debug(`Portal listening on ${PORT}`);
});
that means when i make any calls /api/endpoint it will redirect to localhost:4000/endpoint but will be seen in the network as http://localhost:3000/endpoint1
I want the same behaviour with websockets as well.
I am using new WebSocket(ws://localhost:3000/endpoint1); It should redirect to ws://localhost:4000/endpoint1.
and should be shown in network tab as ws://localhost:3000/endpoint1
Resolved it by using another library http-proxy-middleware
import httpproxy from "http-proxy-middleware";
import proxy from "express-http-proxy";
const app = express();
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3001;
const wsProxy = httpproxy('/ws', {
target: 'ws://localhost:4000',
pathRewrite: {
'^/ws' : '/', // rewrite path.
'^/removepath' : '' // remove path.
},
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
ws: true, // enable websocket proxy
logLevel: 'debug'
});
app.use(wsProxy);
app.use(
"/api",
proxy("http://localhost:4000/", {
proxyReqOptDecorator(opts) {
opts.headers["x-forwarded-host"] = "http://localhost:4000/";
return opts;
}
})
);
app.post("/logger", (req, res) => {
logger.debug(req.body.data);
res.send({ status: "SUCCESS" });
});
app.listen(PORT, () => {
logger.debug(`Portal listening on ${PORT}`);
});