phantom phantom - 10 days ago 7
Node.js Question

deploying node add on server without using localhost

I have a node app that I am trying to deploy on my server. I have an

index.html
file in a public folder and an
app.js
file. If I navigate to the project in the command line and run
node app.js
it runs the app on
localhost:8888
and shows the
index.html
file.

Now that I have uploaded this to my server I am wondering what I need to do, and change (if anything) in my
app.js
file so that i can visit the site without visiting
localhost:8888
, but instead the actual url.

I have tried
http://162.xx.xxx.xxx/folderName/app/public:8888
, but this doesn't work.

var express = require('express')
var app = express();
app.use(express.static(__dirname + '/public'))

app.get('/', function (req, res) {
res.send('Hello World!')
})

app.listen(8888, function () {
console.log('Example app listening on port 8888!')
});

Answer

"Server" is a word with two primary meanings in software development.

It can mean either "A piece of software that listens on a network" or "A computer running that kind of software".

So having uploaded the JavaScript program to the remote computer that is your server you need to do exactly the same as you did on your own computer.

i.e. you need to get a terminal on the server and run node app.js

It will then be available at http://your.example.com:8888/

(More advanced uses would involve using software like forever or system.d to run it automatically as a background process).


If you were using the term server with the other meaning (i.e. you mean "Apache HTTP" or "IIS" or similar), then you are out of luck.

Using Node for server side code means running a server written in JavaScript.

To use this in combination with something like Apache, you would either:

  1. Run the Node server instead of Apache
  2. Run the Node server on a different port and point some services at that port explicitly
  3. Run the Node server on a different port and use something like ProxyPass to have Apache relay requests to it