繁体   English   中英

在Web服务器上部署nodejs app

[英]Deploying nodejs app on a web server

我是NodeJS的新手并且学习,我想知道如何使用NodeJS制作示例应用程序。 我在localhost上试过它,它正在工作。 现在我试图将它公开给任何服务器,但它无法正常工作。

 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

但它没有用。

它其实很简单。 只是不要使用任何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!');

此外,正如萨克所说,你不能只是将其上传到网络服务器。 你必须安装node和npm,你必须实际运行代码

$ nodejs application.js

@Annanta 127.0.0.1:1337是本地机器的ip地址。 请删除此。 请在下面找到示例代码。 尝试使用远程服务器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('Server running');

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

只需编辑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>

暂无
暂无

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

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