Certification authority invalid - node.js

i am trying to send an https request from my frontend (reactjs) to backend (nodejs/express).
These two both run in localhost.
Back end server code:
const app = require('./app')
const https = require('https');
const fs = require('fs');
const credentials = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
//connect to the database
require('./db')
const port = 8765;
app.get('/', (req, res) => {
res.send('Now using https..');
});
var server = https.createServer(credentials, app);
//var server = https.createServer(app);
// listen for requests
server.listen(port, () => {
console.log("server starting on port : " + port)
});
front end request:
const {data: Sessions}= await axios.get("https://localhost:8765/...");
doint this request from postman with the exact same parameters produces the desired result.However when i try to do this from frontend i get:
GET https://localhost:8765/... net::ERR_CERT_AUTHORITY_INVALID in react chrome extention.
Why is this happening and how can i solve this?

Related

How to resolve server error in Socket.io?

I am creating a web application, using socket.io . A Server error occurred while connecting to the server. We found out that the error is in the backend. What could be written incorrectly here? Code:
const path = require('path');
const express = require('express');
const app = express();
const fs = require("fs");
var privateKey = fs.readFileSync('path').toString();
var certificate = fs.readFileSync('path').toString();
const http = require('https').Server({key:privateKey,cert:certificate}, app);
const io = require('socket.io')(http);
const port = 9998;
const debug = true;
var connectedArray = new Array()
const delay = 60 * 1000
const mysql = require('mysql2');
const db = mysql.createConnection({
host: 'localhost',
user: 'user_name',
password: 'user_password',
database: 'database',
});
io.on('connection', (socket) => {
socket.on('register', msg => {
console.log("User registered")
connectedArray.push({
connectmessage: msg,
socket: socket,
})
})
socket.on('disconnect', () => {
if (debug) console.log('User disconnected')
})
})
app.use(express.static(path.resolve(__dirname, 'static')))
app.get('/', (req, res) => {
res.sendFile('./index.html')
})
http.listen(port, () => {
console.log(`Server started listening on port ${port}...`)
})
P.S: The problem began to arise after binding the domain
P.S 2: I have two sites on server, on different Apache virtual hosts
P.S 3: I am using https

How to create a connectable websocket server on Azure app services?

I'm currently running a node and websocket server on azure app services, server looks like this
const http = require('http');
const app = require("express")();
const sqlite3 = require('sqlite3').verbose();
const path = require('path')
const serveStatic = require('serve-static')
const url = require('url');
const httpServer = http.createServer((req, res) => {
res.writeHead(200, {"Content-type": "text/plain"});
res.end("Hello");
});
const port = process.env.PORT || 8080;
httpServer.listen(port, () => console.log("Listening.. on 80"));
const websocketServer = require("websocket").server
const wsServer = new websocketServer({
"httpServer": httpServer
})
wsServer.on("request", request => {
//connect
const connection = request.accept(null, request.origin);
connection.on("open", () => console.log("opened!"))
connection.on("close", () => console.log("closed!"))
connection.on("message", message => {
const result = JSON.parse(message.utf8Data)
})
var payLoad = {
"method": "connect",
"thing": "TEST"
}
connection.send(JSON.stringify(payLoad));
})
However anytime I try to connect I get
(index):79 WebSocket connection to 'ws://triverserver-50.azurewebsites.net/' failed: Error during WebSocket handshake: Unexpected response code: 503
I saw that docker was started on port 8080 in the logs and have tried adding WEBSITES_PORT:80 and PORT:80 to general settings but they had no effect. Any Ideas?

express-ws: "ws.send is not a function"

I'm creating an App using Node.js + Express + Express-ws, but I'm getting the following error when I try to send a message after connection:
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();
/*
const key = fs.readFileSync('./security/server-key.pem', 'utf8');
const cert = fs.readFileSync('./security/server-crt.pem', 'utf8');
const ca = fs.readFileSync('./security/ca-crt.pem', 'utf8');
const credentials = {key: key, cert: cert, ca: ca};
var httpsServer = https.createServer(credentials, app);
*/
var httpServer = http.createServer(app);
httpServer.listen(8443, function(){
console.log('Listening on *:8443 \n');
});
httpServer.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: ' + message);
ws.send(message);
});
ws.send('Hi there, I am a WebSocket server');
});
//ROUTES
app.get('/charger/:id', function(req, res){
res.send('<h1>Hello ' + req.params.id + '</h1>');
});
app.get('*', function(req, res){
res.status(404).send('404 — Not Found');
});
The commented part is to verify that I can use later HTPPS without changint too much stuff.
The error is the following:
TypeError: ws.send is not a function
at Server.<anonymous> (index.js:26:5)
at Server.emit (events.js:194:15)
at TCP.onconnection (net.js:1517:8)
From what I can tell, it's because you're not actually using the express-ws package anywhere. Additionally, the httpServer object is an instance of the http.Server class, and has no built-in knowledge of websockets. Even though you call the argument in the callback ws, it's not actually a websocket object -- it's an instance of the http.ClientRequest class, which has no send method, hence the ws.send is not a function error. So, to resolve this, I think you'll need to do something along these lines, per the docs:
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
app.ws('/', function(ws, req) {
// Now you have a ws object available
})
Hope that helps, good luck!

MEAN stack app not opens from instagram profile

So basicly I posted my MEAN stack app website url on my Instagram profile, but when i try to open it it shows blank page, but it can be open on other browsers. Also Im trying to make Ionic app with InAppBrowser and it shows blank page. Can someone try to explain me what i need to do? My server.js
const https = require('https');
const http = require('http');
const fs = require('fs');
const app = require('./app');
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
};
const https_port = 443;
const server_https = https.createServer(options, app);
server_https.listen(https_port,"0.0.0.0");
const http_port = 80;
const server_http = http.createServer(
function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
});
server_http.listen(http_port,"0.0.0.0");
I just added CA in SSL options
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt'),
ca: fs.readFileSync('./server.ca')
};

Node.js server just keeps loading with no result

server.js
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer();
server.listen(port);
app.js
const express = require('express');
const app = express();
const productRoutes = require('./api/routes/products');
app.use('/products', productRoutes);
module.exports = app;
so when i just run the code node server.js it just keep looping without any result.
Check out this link It gives a bit more detail on how that works.
const http = require('http');
const net = require('net');
const url = require('url');
// Create an HTTP tunneling proxy
const proxy = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');
});
proxy.on('connect', (req, cltSocket, head) => {
// connect to an origin server
const srvUrl = url.parse(`http://${req.url}`);
const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'\r\n');
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});
// now that proxy is running
proxy.listen(1337, '127.0.0.1', () => {
// make a request to a tunneling proxy
const options = {
port: 1337,
hostname: '127.0.0.1',
method: 'CONNECT',
path: 'www.google.com:80'
};
const req = http.request(options);
req.end();
req.on('connect', (res, socket, head) => {
console.log('got connected!');
// make a request over an HTTP tunnel
socket.write('GET / HTTP/1.1\r\n' +
'Host: www.google.com:80\r\n' +
'Connection: close\r\n' +
'\r\n');
socket.on('data', (chunk) => {
console.log(chunk.toString());
});
socket.on('end', () => {
proxy.close();
});
});
});
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer();
server.listen(port);
As far I could extract from code, the thing that is most probably happening in your server.js is that your server is waiting for some request. And you have nothing in your code handle requests.
I think you have to call the require function to return the actual router object, try to change this line
from: const productRoutes = require('./api/routes/products');
to: const productRoutes = require('./api/routes/products')();

Resources