【问题标题】:How to access the Node server hosted in my laptop from a device in another network?如何从另一个网络中的设备访问托管在我的笔记本电脑中的节点服务器?
【发布时间】:2020-07-12 17:40:34
【问题描述】:

我已经写了这个 test.js

var http = require("http");
http.createServer(function (request, response) {  
// Send the HTTP header   
// HTTP Status: 200 : OK  
// Content Type: text/plain  
response.writeHead(200, {'Content-Type': 'text/plain'});  
// Send the response body as "Hello World"  
response.end('Hello World\n');  
}).listen(8080,'192.168.1.5');  
// Console will print the message  
console.log('Server running at http://192.168.1.5:8080/');   

我运行了命令 node test.js

C:\Users\ShalomAlexander\Documents>node test.js
Server running at http://192.168.1.5:8080/

可以从我的笔记本电脑和连接到同一 wifi 网络的设备访问此 URL。但是当我在智能手机上从 wifi 网络切换到移动网络时,我无法访问它。

那么我怎样才能让它在不同网络中的其他设备上工作呢?

【问题讨论】:

  • 好吧,您只需将其托管在公共服务器上,而不是本地主机上。 (当我说“简单”时......)

标签: javascript node.js server request response


【解决方案1】:

你需要...

  1. 将您的应用托管在公共服务器(例如云提供商)上或
  2. 公开您的机器

第二个选项要求您打开端口并从路由器正确转发到您的机器。此外,您可能需要一些动态 DNS 条目作为您的 IP,因为通常您没有固定 IP。只是一些提示 - 我不知道你的用例是什么。

【讨论】:

    【解决方案2】:

    您需要在服务器上托管您的 NodeJs 应用程序(例如:heroku)。然后您可以使用托管 URL 访问它。

    请查看有关在 Heroku 中部署 NodeJs 应用程序的文档。

    https://devcenter.heroku.com/articles/getting-started-with-nodejs

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2019-07-19
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2010-12-16
      • 1970-01-01
      相关资源
      最近更新 更多