【问题标题】:How to not use localhost, but the IP of the server instead?如何不使用localhost,而是使用服务器的IP?
【发布时间】:2021-02-16 16:54:28
【问题描述】:

我在this tutorial 上找到了这个节点应用程序并想使用它,但它被配置为作为本地主机运行。由于应用程序在 Amazon Linux EC2 上运行,因此没有桌面(本地)访问权限(可能需要安装软件来启用桌面模式,但我没有)。

我想在服务器上运行应用程序,而不是在本地主机上,而是在服务器的弹性 IP 地址上运行,我将把它添加到我的域 chatxs.com 的托管区域中。

我想让应用侦听此 IP 中的任何请求,该 IP 将再次位于域名的 DNS 中。

这是教程附带的代码,我唯一更改的是视图文件夹中的 .html 文件(样式、对齐和一些文本,应用程序没有代码更改,只是 html):

app.js

// This is the main file of our chat app. It initializes a new 
// express.js instance, requires the config and routes files
// and listens on a port. Start the application by running
// 'node app.js' in your terminal

var express = require('express'),
    app = express();

// This is needed if the app is run on heroku:

var port = process.env.PORT || 8080;

// Initialize a new socket.io object. It is bound to 
// the express app, which allows them to coexist.

var io = require('socket.io').listen(app.listen(port));
// Require the configuration and the routes files, and pass
// the app and io as arguments to the returned functions.

require('./config')(app, io);
require('./routes')(app, io);

console.log('Your application is running on http://localhost:' + port);

config.js

// This file handles the configuration of the app.
// It is required by app.js

var express = require('express');

module.exports = function(app, io){

// Set .html as the default template extension
app.set('view engine', 'html');

// Initialize the ejs template engine
app.engine('html', require('ejs').renderFile);

// Tell express where it can find the templates
app.set('views', __dirname + '/views');

// Make the files in the public folder available to the world
app.use(express.static(__dirname + '/public'));

};

routes.js 太大,无法在此处格式化。

最后

package.json

{
"name": "NodeChatSystem",
"version": "0.0.1",
"description": "Realtime chat system for Tutorialzine.com",
"main": "app.js",
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
  "node",
  "chat",
  "system"
],
"author": "Nikolay Anastasov",
"license": "MIT",
"dependencies": {
  "ejs": "^1.0.0",
  "express": "^4.8.2",
  "gravatar": "~1.0.6",
  "socket.io": "^1.0.6"
  }
}

以上基本上是教程.zip文件中包含的内容。

【问题讨论】:

  • 只需在52.19.7.59 的服务器上部署您的应用程序(即将所有代码放在该服务器上并运行应用程序)。当您点击该 IP 地址 + 您指定的端口时,您将点击该应用程序。
  • 如果您在 AWS 上运行它应该可以正常工作。如果您尚未这样做,请确保您在 AWS 控制台中允许访问相关端口上的传入流量。
  • @Houseman 感谢您的快速反馈。我很尴尬地问这个问题,但是如何部署它,我对这些东西感到困惑,但我真的希望它能够启动并运行。到目前为止我只了解 html css :(
  • @Darth_nVade_Her 如果您希望在 EC2 服务器上部署和运行 node.js,此链接 adndevblog.typepad.com/cloud_and_mobile/2014/12/… 应该会对您有所帮助。随着 MattHouser 的回答,您应该能够运行 nodejs 应用程序
  • @Raghav 我实际上使用本教程来设置服务器,之后我所做的只是编辑教程 html 并上传它。然后我运行“npm install”,一旦完成“node app.js”

标签: node.js amazon-web-services amazon-ec2 socket.io


【解决方案1】:

当您简单地使用app.listen(port) 时,它将只允许来自本地主机的连接(如您所见)。

要允许外部连接,请使用app.listen(port, "0.0.0.0")。这告诉 Node 监听外部网络接口。

另外,请确保您的 EC2 实例的安全组允许在适当的端口上进行传入连接。

更新:

正如 jfriend00 所指出的,进一步调查,这是不正确的。

参考:http://expressjs.com/api.html#app.listen

【讨论】:

  • 我做到了,当我运行它时,出现了 imgur.com/GzkcxTh 。我也应该有外括号吗?像 (app.listen(port, "0.0.0.0")) 或者你说的那样?
  • 您的服务器正在端口上运行。该错误看起来像是某种运行时错误(不是编译或语法错误)。
  • 尝试捕获错误并打印它以查看它是什么。
  • 这根本不正确。 app.listen(port) 不限制连接只能来自本地主机。我有一台使用app.listen(port) 运行的服务器,它接受来自我网络上任何主机的连接。
  • @Darth_nVade_Her 你能验证端口 8080 上是否已经有东西在运行吗?
【解决方案2】:

首先,我认为您对网络和 IP 地址感到困惑。 localhost 是 IP 地址 127.0.0.1 的保留主机名,相当于 THIS COMPUTER。您可以在 official wiki page 上阅读有关 localhost 的更多信息。

其次,我强烈建议您删除您的 EC2 实例,暂时不要使用它。如果您是初学者,有很多免费且用户友好的选项。例如 Heroku 是一个很好的开始平台。我强烈建议您从他们的教程开始 - Getting Started with Nodejs。首先尝试在那里开发简单的应用程序并部署到 Heroku。一些无意的错误(在 Github 上公开配置和密码、打开端口、公开公共 IP 地址等)在 EC2 上可能会在几分钟内花费您数千美元。因此,一旦您更有经验,我建议您稍后再回到 EC2。 Heroku 对一个应用程序也是免费的,因此您无需支付任何费用,而且设置起来非常简单。您也可以设置自己的域。

当你说:

我想做的基本上是在服务器上运行应用程序, 不在本地主机上,而是在服务器的弹性 IP 地址上,我将 添加到我的域 chatxs.com 的托管区域。

我假设您正在尝试将您的应用程序发布到您自己的服务器上。我不会一步一步地编写如何将 Node 应用程序部署到服务器,我认为您应该使用其他托管。但是,如果您想了解更多关于在生产环境中运行 Node 应用程序的信息,我认为 this 教程可以让您了解所有需要的内容。

最后,应该提到的是,您永远不应该在互联网上公开敏感信息。从您的问题中删除公共 IP 地址。

【讨论】:

  • 好的,谢谢你的详细回答,我太兴奋了,直接进入了专业的东西。现在的婴儿步骤:D 我会查看教程,看看我能做什么。再次感谢! :)
  • @Darth_nVade_Her,别担心,我们都去过那里。祝你好运!
  • 呃,从什么时候开始有公共 IP 地址敏感信息?它是公开的。
  • @Darth_nVade_Her:在私有子网中使用 vpc,前端有一个 elb。
【解决方案3】:

这对我有用.. 只允许 tcp 连接到端口, 使用以下命令sudo ufw allow 5000/tcp 这里我使用了 5000 端口, 根据您的端口号更改它。 参考:https://blog.cloudboost.io/deploying-a-node-js-express-application-on-digital-ocean-part-1-5d5b0cfe0a34

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多