Using express server as electron app proxy for HTTPS requests - node.js

I'm using session.setProxy to route every request through my basic express server.
I want to load a webpage and replace the page content with my own content.
If I load a http url the content from the express server is loaded but if I use https the app is just blank.
Here's the main.js of my electron app:
const electron = require('electron')
const { app, BrowserWindow } = electron
var win;
app.on('ready', initwindow);
function initwindow() {
win = new BrowserWindow({show:true});
win.webContents.session.setProxy({
proxyRules: 'https://xxx',
}, () => {
win.loadURL('https://www.google.com/');
});
}
Here's my express server:
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();
const credentials = {
key: fs.readFileSync('/etc/letsencrypt/live/xxx/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/xxx/fullchain.pem'),
};
app.get('/', function(req, res) {
res.send('Hello world')
})
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
For example, if I use win.loadURL('http://www.google.com/'); "Hello world" is displayed, but if I use win.loadURL('https://www.google.com/'); the electron app is just white.
Is there something missing from my server or is what i'm trying to do no possible?

Related

socket.io server doesn't work after deployment

I'm having trouble with connect to my socket.io server, I want user to be connected when on.connection, here's my server code
const express = require('express');
const http = require('http');
const socketio = require('socket.io');
const cors = require('cors');
const {getUserList, get_Current_User, user_Disconnect, join_User, playByTurn } = require("./dummyuser");
//Set static folder
const app = express();
app.use(cors());
const server = http.createServer(app);
const io = socketio(server, {
cors: {
origin: "http://localhost:8080",
methods: ["GET","POST"]
}
});
const PORT = process.env.PORT || 8000;
//Run when client connect
//Run when client connect
io.on('connection', socket => {
console.log(`Connected: ${socket.id}`);
//Code
});
socket.on('disconnect', () => {
console.log(`Disconnected: ${socket.id}`);
//Code
})
});
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
On my localhost it works just fine, but after I deploy it said that it can't GET from my server, any ideas? Thanks
I was finally solved it by adding transports: ['websocket'] in my client code
import io from 'socket.io-client';
const socket = io("//url",{
transports: ['websocket'],
});

Getting unexpected CORS error with socket.io and React

I don't know what else to try
My server:
const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app)
const cors = require('cors');
app.use(cors());
app.options('*', cors());
const socketio = require('socket.io')
const io = socketio(server);
io.on("connect", socket => {
console.log("hello");
socket.on("welcome ", (e) => {
console.log(e)
})
});
const port = 8081;
server.listen(port, () => console.log(`listen on port ${port}`))
in app react
import io from "socket.io-client";
let socket = io.connect("http://localhost:8081");
socket.emit("welcome", "connected");
react run port 3000
nodejs run port 8081
but everything I try throws error.

Self host Bitbucket with SSL using Express

I can get bitbucket to run at localhost:7990 just fine however I wanted to secure it with a SSL and also remove the port from the address. When I attempt to do this with the code below and my node server I just simply get the 'CANNOT GET /' error if I goto https://example.com. Even though the bitbucket app is running.
// Dependencies
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const express = require('express');
const express_force_ssl = require('express-force-ssl');
const app = express();
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use(express_force_ssl);
// Load main application
app.use(express.static(path.join(__dirname, '../../var/www/bitbucket/')));
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, (req,res) => {
console.log('HTTPS Server running on port 80');
});
httpsServer.listen(443, (req, res) => {
console.log('HTTPS Server running on port 443');
});

Console log not logging with express and socket.io

I'm having trouble understanding how web sockets work. I'm not seeing my console.log "connection" inside the io.on function when I visit http://www.localhost:3010 in my browser. Could someone explain why that is?
server.js:
const app = require("./backend/app")
const http = require('http');
const server = http.createServer(app)
var socketIo = require('socket.io')
server.listen(3010, () => {
console.log("listening")
})
const io = socketIo(server)
io.on("connection", (socket) => {
console.log("connection")
socket.emit("news", {hello: "world"})
})
app.js:
const express = require("express");
const app = express();
app.get("/", (req, res, next) => {
res.send("hello world");
})
module.exports = app

Express route returning 404 error

The express route in my node app is returning a 404 error. It works locally but on the production server it returns a 404 error (bear in mind that I changed the AJAX url to the one on my production server before uploading). My code:
server.js:
const path = require('path');
const http = require('http');
const express = require('express');
const publicPath = path.join(__dirname, '/../public');
const port = process.env.PORT || 8080;
var app = express();
var server = http.createServer(app);
app.use(express.static(publicPath));
app.post('/active-screens', function(req, res) {
res.send('route works!');
});
server.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
index.js:
function checkRoute(){
$.ajax({
url: "http://localhost:8080/active-screens",
type: "POST"
})
.success(function(data){
console.log(data);
});
}
checkRoute();
It was a CORS issue as pointed out by #WejdDAGHFOUS.
I updated my server.js to this:
const path = require('path');
const http = require('http');
const express = require('express');
const cors = require('cors');
const publicPath = path.join(__dirname, '/../public');
const port = process.env.PORT || 8080;
var app = express();
var server = http.createServer(app);
app.use(express.static(publicPath));
app.post('/active-screens', cors(), function(req, res) {
res.send('route works!');
});
server.listen(port, () => {
console.log(`Server is up on port ${port}`);
});

Resources