如何不使用本地主机,而是使用服务器的IP? [英] How to not use localhost, but the IP of the server instead?

查看:190
本文介绍了如何不使用本地主机,而是使用服务器的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本教程上找到了该节点应用程序,并想使用它,但已配置为以localhost身份运行。由于该应用程序在Amazon Linux EC2上运行,因此无法对其进行桌面(本地)访问(也许要安装软件以启用桌面模式,但我还没有安装)。

I found this node application on this tutorial and wanted to use it, but it's configured to run as localhost. Since the application is run on Amazon Linux EC2, there is no desktop (local) access to it (maybe there's software to install to enable desktop mode, but I haven't got that).

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

I'd like to run the application on the server, not on localhost, but on the server's Elastic IP address which I'll be adding to a Hosted Zone of my domain chatxs.com.

我想让该应用程序侦听此IP中的任何请求,该IP同样位于域名的DNS中。

I'd like to make the app listen for any requests in this IP, which again, will be in the domain name's DNS.

这是本教程随附的代码,我唯一更改的是views文件夹中的.html文件(样式,对齐方式和一些文本,应用程序无需更改代码,只是html):

Here's the code the tutorial comes with, the only thing I've changed is the .html files in the views folder (styling, alignment and some text, no code changes of the app, just the 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'));

};

route.js 太大了,无法在此处设置格式。

routes.js it was too big to format it here.

最后

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文件中包含的内容。

The above is basically what's included in the tutorial .zip file.

推荐答案

首先,我认为您对网络和IP地址感到困惑。 localhost 是IP地址127.0.0.1的保留主机名,等效于说此计算机。您可以在官方Wiki页面上了解有关本地主机的更多信息。

First of all, I think that you are confused about networking and IP addresses. The localhost is reserved hostname for IP address 127.0.0.1 and is equivalent to say THIS COMPUTER. You can read more about localhost on official wiki page.

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

Secondly, I would highly encourage you to delete your EC2 instance and don't use it for now. If you are beginner, there is plenty of options which are for free and are more user friendly. For example Heroku is great platform to start on. I highly recommend you to start with their tutorial - Getting Started with Nodejs. Try first develop simple app there and deploy to Heroku. Some unintentional mistakes(expose configuration and passwords on Github, opening Ports, exposing public IP addresses, etc.) on EC2 can costs you thousands of dollars in period of few minutes. So I would recommend to get back to EC2 later, once you are more experienced. Heroku is also free for one app, so it won't cost you anything and it is really simple to setup. You can also setup your own domain.

说:


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

What I'd like to do is basically run the application on the server, not on localhost, but on the server's Elastic IP address which I'll be adding to a Hosted Zone of my domain chatxs.com.

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

I assume that you are trying to release your application to your own server. I'm not going to write step by step how to deploy Node app to a server, I think you should use some other hosting. But if you would like to know more about running Node app in production environemnt, I think that this tutorial can give you glimpse of what is all needed.

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

Finally, it should be mentioned, that you should never, ever expose sensitive information on the internet. Delete the public ip address from your question.

这篇关于如何不使用本地主机,而是使用服务器的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆