如何不使用localhost,而是使用服务器的IP



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

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

我想让应用程序侦听该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文件中包含的内容。

当您简单地使用app.listen(port)时,它将只允许来自localhost的连接(正如您所看到的)。

要允许外部连接,请使用app.listen(port, "0.0.0.0")。此操作会告诉节点在外部网络接口上侦听。

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

更新:

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

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

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

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

当你说:

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

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

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

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

最新更新