How to run node.js on my website server and not on local pc server

The last 2 days I spent more time and read more than 50 articles and videos to understand node.js, and after installation I can now see the result in the browser http//:localhost:3000/ But I mixed up a lot, which I describe below.

I do all my work on my hosting server, where I save my website: www.myweb.com

In each article about node.js, they teach how to get the result using the code below in the browser http//:localhost:3000/ on the local PC.

test.js

 var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(3000); console.log('Server running at http://localhost:3000/'); 

But my question is:

  • If I use http //: www.myweb.com / test.js` in my browser, what will be the above code?

  • In the case of a local PC, we write on npm node test.js , but in the case of a hosting server, when a client opens a page, for example, http//:www.myweb.com/test.js How does it work?

  • If using php, enable ("head.php") to get something from this page. But in this case, How to make a call to node.js.

+7
javascript php
source share
2 answers

Well, what you need to do is understand how http web servers work.

Usually on your remote computer (on your server) an instance of the web server is running (for example: apache), which listens on port 80 (the standard port for HTTP requests). It will process every request made on this port and manage the routing to use the correct php / html file.

Then it will run the php code on the server side to display the html file and transfer it to the server. Thus, the client will not see php code at all.

Tell us about Node.js. Node is an application that runs JavaScript code on a server, and can run an http server using some modules. But the javascript code will never be shown to your client, it will receive only the HTTP response that you send to it (usually an html page).

So now with Node.js you need to do the same thing as the Apache server by creating an http server. First, you should be aware that not many website hosts offer Node.js or even console access. Usually they serve the php / html files that you put in the configured folder, and that is basically it. You will need either a virtual machine or a server on which you can install Node.js and run it, or use a Node.js hosting service, for example heroku or nodejitsu, to host your Node.js http server.

So, to create the Node.js http server, you need to create an http server (as in your code) and make it listen on port 80. Now, every HTTP request sent to your server will be processed by your Node.js. Then you can do whatever you want with this request.

I hope I was not dirty.

+4
source share

You need to install NodeJS on the server. If this is shared hosting, where you cannot install additional software, then you will not be able to use NodeJS. In this case, contact your web hosting support team and ask for NodeJS support.

On the other hand, if you have root or superuser privileges on the system, you can install NodeJS. For example, for CentOS / RHEL systems, you can install the following commands with yum.

 sudo yum install epel-release sudo yum install npm 

For some other Linux distributions: http://ask.xmodulo.com/install-node-js-linux.html

To access Node applications from your PC to the server, you also need to open the port on the server firewall that your Node program uses.

0
source share

All Articles