【发布时间】: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