0

When I enter www.example.com:3000 in the browser, I receive this error (where 'example' is the name of my domain)

This site can't be reached - www.example.com took too long to respond.

I have done these things:

  1. Installed node.js on my GoDaddy shared account
  2. Created a folder ../public_html/testsite
  3. Placed two files in that folder: app.js and .htaccess.
  4. Start webserver with: node app.js
  5. Go to browser and enter my domain's URL and port:
  6. Receive the error message above

This post is very helpful, but I still cannot get my set up to work.

These two files are in ../public_html/testsite/

.htaccess
RewriteEngine on
RewriteRule ^index.html.var$ http://www.example.com:3000/$1 [L,P,QSA]
RewriteRule (.*) http://www.example.com:3000/$1 [P,L]

Note: index.html is the file that normally loads when you visit here

The app.js:

const http = require('http');

const hostname= '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('NodeJS server running on Shared Hosting\n');
});

server.listen(port, hostname, () => {
  console.log('Server running at http://' + hostname + ':' + port + '/');
});

To start the webserver:

> cd ../public_html/testsite
> node app.js
Server running at http://127.0.0.1:3000

In browser, I enter:

www.example.com:3000

I expected to see, in the browser

NodeJS server running on Shared Hosting

Questions:

  1. Should I use my own godaddy domain address for hostname in .htaccess or should it be localhost?
  2. Is the idea to redirect www.example.com:3000 (in browser) to http://localhost:3000 (on GoDaddy server)?
  3. I've tried all sorts of permutations (using my ip address or domain name, different port numbers, etc.)
  4. I think I am close, but need a few ideas to try!
Bear
  • 662
  • 1
  • 5
  • 20
THarron
  • 1
  • 1
  • 2
  • The instructions you're linking to have the two files directly in the `public_html` folder. I guess you should try moving them there. I also think you should just go to example.com, without port 3000. What you have right now could work if you go to `example.com/testsite` –  Jan 17 '19 at 17:08

1 Answers1

0

Port 3000, 8080, and the likes are normally used for development purposes, as in development in can be useful to have several servers running at the same time, for example one on port 3000, one on port 3001, etc.

However, on the internet, HTTP is served on port 80 and HTTPS is served on port 443. So basically, in your server implementation, you should set the port dynamically: it must not be the same whether you're running in production and in development!

I personally use the fact that on my production environment (ie for you, GoDaddy's deployment machines), the environment variable PORT is already set to 80, whereas on my local machine I don't set it, so I can write this:

const express = require('express');
const port = process.env.PORT || 3000; // 3000 on my machine, 80 on GoDaddy's server
const app = express();
app.listen(port, () => console.log(`App listening on port ${port}`));

And I access the server at these URLs:

http://localhost:3000/
http://example.com:80/
http://example.com/

The last two are the same because, as previously said, default HTTP port is 80.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • When enter http://example.com:80, it simple loads the index.html for the website does not access the webserver running on the GoDaddy server). The website has client html and client-side javascript already. I want to add a variety of services (presumably accessed using the same website url + port number). – THarron Jan 17 '19 at 19:47
  • It's really unclear what you're trying to do. In short, you won't be able to deploy a website on GoDaddy that serves on a port different than 80. Is there something you don't understand? – Nino Filiu Jan 18 '19 at 09:10
  • There are definitely things I don't understand (that's why I posted the question, actually :)). I do have an .html file with embedded ajax calls, calling various express route endpoints now! Your comment about the server being hosted at ONE port, was helpful. I was under the misunderstanding route endpoints were mapped to a specific port (e.g. www.example.com:8000 to have access to routes) - that was my main confusion. I am going to prepare an end to end tutorial of the steps I took to get this working - I look forward to your comments on that. – THarron Jan 18 '19 at 20:33
  • This seems to involve many files and configs. Don't forget ta add an [mcve](https://stackoverflow.com/help/mcve) ;) – Nino Filiu Jan 18 '19 at 22:18