在Web服务器上部署nodejs应用程序

我是NodeJS的新手和学习,我想知道如何使用NodeJS制作示例应用程序。 我已经在localhost上试过了,它正在工作。 现在我试图将其公开托pipe到任何服务器,但是它不工作。

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

这是代码保存在我的本地机器为m.js,我运行:

 $ node m.js 

它运行良好,当我在浏览器中打开它为https://127.0.0.1:1337

我得到的输出为:

 Hello World 

我想从远程服务器做同样的事情,即我有一个域名www.example.com ,如果我打开它与https://www.example.com:1337那么它应该显示在我的浏览器屏幕上像以前一样:

 Hello World 

但它不工作。

@Annanta 127.0.0.1:1337是本地机器的IP地址。 请删除这个。 请在下面find示例代码。 尝试使用远程服务器ipaddress访问它。 请注意,远程机器必须安装节点和npm。

 var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(1337); 

console.log('服务器运行');

以下是使用Apache2-Webserver的可能解决方法:

只需在conf.d中编辑虚拟主机(对于Ubuntu,您将在/etc/apache/find它),运行a2enmod proxy并重新启动Apache。

这是一个可能的configuration:

 NameVirtualHost *:80 <VirtualHost *:80> ServerName your-domain.com ServerAlias www.your-domain.com ProxyRequests off ProxyPass / http://127.0.0.1:1337/ ProxyPassReverse / http:/127.0.0.1:1337/ </VirtualHost> 

其实很简单。 只要不使用任何IP,只需定义端口即可。

 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337); console.log('Server running!'); 

同样如Sach所说,你不能只上传到networking服务器。 你必须安装node和npm,你必须通过运行代码

$ nodejs application.js