Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Redirect

I have some experience of using Node.js on a VPS but I am trying to set up an app on shared hosting. I can get it to respond when I access it via the URL but when I try to connect from a browser the request is being 301 redirected but I have no idea why. The techs on the shared hosting are trying to help but admit they are not Node.js savvy.

I am creating the socket within the HTML page and the link is working OK. If I insert a none existent URL, it gives an error, as you would expect.
socket = new WebSocket('wss://www.<domain>.co.uk:443');


My question is how do I dig into this deeper to get to the bottom of the mystery?
 
I have some experience of using Node.js on a VPS but I am trying to set up an app on shared hosting. I can get it to respond when I access it via the URL but when I try to connect from a browser the request is being 301 redirected but I have no idea why. The techs on the shared hosting are trying to help but admit they are not Node.js savvy.

I am creating the socket within the HTML page and the link is working OK. If I insert a none existent URL, it gives an error, as you would expect.
socket = new WebSocket('wss://www.<domain>.co.uk:443');


My question is how do I dig into this deeper to get to the bottom of the mystery?
Hi there,

Can you clarify one thing for me?
Code:
I access it via the URL but when I try to connect from a browser the request is being 301 redirected but I have no idea why
Not sure what you mean by "access it by the URL"..are you referring to using the curl command on terminal/command prompt?
 
Sorry if I have the terminology wrong but I will try to explain.
I have followed a number of tutorials which deal with how to set up Node via Plesk, on a remote server. I have managed to set it up and when I enter the domain name of the remote Node site in a browser the Node app runs and sends output back to the browser.

I now want to set up sockets to connect to the Node server, as per the call in my original post. This is the calling line of code and this sits in script tags, within the HTML of a web page, with the rest of the socket code. The same code is already working on the VPS but I don't want to confuse the issue because the problem lies in the simple action of connecting to the server. Instead of connecting or returning a valid error, it just reports that it cannot establish a connection and the remote hosts are clueless as to why this simple process doesn't work.
 
It may help if I provide the code I am using, I have trimmed this down, to the bare bones from much larger scripts, so the code is not complete but basic functionality should work .
I have taken it back to an insecure connection in order to make it as basic as possible and still it doesn't work for some reason.



Server code
JavaScript:
const http = require('http');
const fs = require('fs');
const ws = require('ws');
let mysql = require("mysql");

const port = process.env.PORT || 80;

let server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Ready to Start the Party\n");
}).listen(port);

console.log(`App is running... (port: ${port})`);

const wss = new ws.Server({ server, path: '' });
console.log('########### W S S '+wss);

wss.on('message', (data) => {
    var u8 = new Uint8Array(data);
    var stdets = String.fromCharCode.apply(null, u8);
    var mess = stdets.split(":");
    console.log('Message in server');
});
   
wss.on('close', (data) => {
    console.log('Close in server');

});

and the HTML/Javascript


HTML:
<!-- ============================================================= -->
<!-- HEADER SECTION REMOVED FOR CLARITY -->
<!-- ============================================================= -->

<body>
<button class='buth' type='button' id='S1' onclick="startbut('5')">Press me</button>
<script>

console.log('Press The Button');

// ===================================================================================
function startbut(b){ //   S T A R T   G A M E   I N   S P E C I F I E D   T I M E
// ===================================================================================
console.log('Trying to connect to server');
    wsconnect('some info');
}

// ==========================================
function wsconnect(mode) {
// ==========================================
    socket = new WebSocket('wss://<domain>.co.uk:80');

    socket.addEventListener("open", (event) => {
          console.log("WebSocket open: ", event);
          console.log("WebSocket open data: ", event.data);
    });
    socket.addEventListener("error", (event) => {
          console.log("WebSocket error: ", event);
          console.log("WebSocket error data: ", event.data);
    });
   
    socket.addEventListener("message", (event) => {
        console.log("Message from server ", event.data);
        console.log("Message from server data", event.data);
    });   

}

</script>
</body>
 
Last edited by a moderator:
I have finally worked out the issue, I am using a mixture of secure and insecure protocols and the server is rejecting the request for a connection. I just wonder why this situation doesn't generate any kind of errors or warnings? It would appear that meaningful error logs and accurate documentation are so last century and only used by old people 🙂
 

New Threads

Buy us a coffee!

Back
Top Bottom