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

Javascript 在web服务器上部署nodejs应用程序,javascript,node.js,Javascript,Node.js,我对NodeJS和学习是新手,我想知道如何使用NodeJS制作一个示例应用程序。 我在localhost上试用过,它正在工作。现在,我试图将其托管到任何服务器上,但它不起作用 var http=require('http'); http.createServer(函数(req,res){ res.writeHead(200,{'Content-Type':'text/plain'}); res.end('Hello World\n'); }).听(1337,'127.0.0.1'); cons

我对NodeJS和学习是新手,我想知道如何使用NodeJS制作一个示例应用程序。 我在localhost上试用过,它正在工作。现在,我试图将其托管到任何服务器上,但它不起作用

var http=require('http');
http.createServer(函数(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello World\n');
}).听(1337,'127.0.0.1');

console.log('服务器在运行http://127.0.0.1/');其实很简单。只需不使用任何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所说,您不能将其上传到Web服务器。您必须安装node和npm,并且必须通过

$nodejs application.js


@Annanta 127.0.0.1:1337是一个本地计算机ip地址。请把这个拿走。 请在下面找到示例代码。尝试使用远程服务器IP地址访问它。请注意,远程计算机必须安装节点和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);

log('Server running')

以下是使用Apache2 Web服务器的可能解决方法:

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

以下是一种可能的配置:

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>
NameVirtualHost*:80
ServerName your-domain.com
ServerAlias www.your-domain.com
代理请求关闭
ProxyPass/http://127.0.0.1:1337/
ProxyPassReverse/http:/127.0.0.1:1337/

谢谢,但当我试图请求时,它无法使用我给你的确切代码。请记住,这只在服务器运行时才起作用。我同意,在我的putty中,它也起作用,显示与附件中的图u相同。但是,当我试图使用浏览器访问它时,它显示网页不可用。使用web服务器的IP是我唯一的想法,老实说:/n不知道,然后tbh。sry:/谢谢,但是当我尝试请求时,它在anks中不工作,但我使用的是centos,您能给我正确的配置吗?您可以在/etc/httpd/中找到conf.d。配置与使用Ubuntu时相同。请确保您正在配置的端口上运行节点应用程序。@ananta您可以将端口更改为80或8080,并验证它是否正常工作。如何为nodejs配置端口