简体   繁体   English

如何在我的网站服务器中运行node.js而不是我的PC本地服务器

[英]How to run node.js in my web site server not my pc local server

Last 2 days I spent more time and read 50+ articles and video to understand node.js and after installation now I can see the result in browser by http//:localhost:3000/ But I have confused in many case that I describe below. 最近2天我花了更多的时间阅读50多篇文章和视频来了解node.js,现在安装后我可以通过http//:localhost:3000/看到浏览器中的结果但​​是我在下面描述的很多情况下都很困惑。

I do all of my work in my share hosting server where I my keep my web site: www.myweb.com 我在我的共享托管服务器上完成了所有工作,我保留了我的网站: www.myweb.com

In every article about node.js, they are teaching how to get a result by below code in a browser by http//:localhost:3000/ in local pc server. 在每篇关于node.js的文章中,他们都在教授如何通过本地pc服务器中的http//:localhost:3000/在浏览器中通过以下代码获得结果。

test.js 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: 但是我的问题:

  1. If I use http//:www.myweb.com/test.js` in my browser, What will be the above code? 如果我在浏览器中使用http //:www.myweb.com / test.js`,上面的代码是什么?

  2. In case of local pc we write on npm node test.js , But In case of hosting server when any clint open the page like http//:www.myweb.com/test.js How to work it? 在本地PC的情况下,我们在npm node test.js上写,但是在托管服务器的情况下,任何clint打开页面如http//:www.myweb.com/test.js //:w​​ww.myweb.com/test.js如何工作?

  3. In case of php we used include ("head.php") to got something from that page But In this case How to make a call on node.js. 在php的情况下,我们使用include(“head.php”)从该页面获取内容但在这种情况下如何在node.js上进行调用。

Well, what you need to do is understand how http web servers works. 那么,您需要做的是了解http Web服务器的工作原理。

Usually, on your remote machine (your server), you have an instance of a web server (ex : apache) running, which is listening to port 80 (standard port for http requests). 通常,在您的远程计算机(您的服务器)上,您有一个运行的Web服务器实例(例如:apache),它正在侦听端口80(http请求的标准端口)。 It will handle every request made on that port, and manage routing to use the correct php/html file. 它将处理在该端口上发出的每个请求,并管理路由以使用正确的php / html文件。

Then, it will run the php code server-side , to render an html file and serve it to the server. 然后,它将运行php代码服务器端 ,以呈现html文件并将其提供给服务器。 So the client will not see the php code at all. 所以客户端根本不会看到php代码。

Let's talk about Node.js. 我们来谈谈Node.js. Node is an application that runs javascript code server-side, and can run an http server with using some modules. Node是一个运行javascript代码服务器端的应用程序, 可以使用某些模块运行http服务器。 But the javascript code will never be shown to your client, he will only get the http response you send him (typically, the html page). 但javascript代码永远不会显示给您的客户端,他只会获得您发送给他的http响应(通常是html页面)。

So now, with node.js, you need to do the same as the apache server did, by creating the http server. 现在,使用node.js,您需要通过创建http服务器来执行与apache服务器相同的操作。 First, what you have to know is that not that many website host are offering node.js, or even console access. 首先,您必须知道的是,没有那么多的网站主机提供node.js,甚至是控制台访问。 They usually serve the php/html files you put in the configured folder, and that's basically it. 它们通常用于放置在配置文件夹中的php / html文件,基本上就是这样。 What you need is either a virtual machine, or a server on which you can install node.js and run it, or use a node.js hosting service, like heroku or nodejitsu to host your node.js http server. 您需要的是虚拟机,或者您可以在其上安装node.js并运行它的服务器,或者使用node.js托管服务(如heroku或nodejitsu)来托管您的node.js http服务器。

So, to create the node.js http server, you need to create an http server (as you did in your code), and make it listen to port 80. Now, every http request send to your server will be handled by your node.js instance. 因此,要创建node.js http服务器,您需要创建一个http服务器(就像您在代码中所做的那样),并使其监听端口80.现在,发送到您的服务器的每个http请求都将由您的节点处理.js实例。 Then, you can do anything you want with that request. 然后,您可以使用该请求执行任何操作。

I hope I haven't been to messy. 我希望我没有变得凌乱。

You need to install NodeJS on the server. 您需要在服务器上安装NodeJS。 If this is shared hosting where you cannot install additional software then you will be unable to use NodeJS. 如果这是无法安装其他软件的共享主机,那么您将无法使用NodeJS。 In that case contact support of your web hosting company and inquire about NodeJS support. 在这种情况下,请联系您的网络托管公司的支持,并询问NodeJS支持。

On the other hand, if you do have root user or super user rights on a system, you can install NodeJS. 另一方面,如果您在系统上拥有root用户或超级用户权限,则可以安装NodeJS。 For example for on CentOS/RHEL systems you can install using yum with the following commands. 例如,对于CentOS / RHEL系统,您可以使用yum使用以下命令进行安装。

sudo yum install epel-release
sudo yum install npm

For some of the other distributions of Linux: http://ask.xmodulo.com/install-node-js-linux.html 对于Linux的其他一些发行版: http//ask.xmodulo.com/install-node-js-linux.html

To access Node applications from your PC to the server, you also need to open a port in the server firewall that your Node aplication uses. 要从PC访问节点应用程序到服务器,还需要在节点应用程序使用的服务器防火墙中打开一个端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM