Reputation: 13
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).
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.
I'd like to make the app listen for any requests in this IP, which again, will be in the domain name's DNS.
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'));
};
routes.js it was too big to format it here.
And finally
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"
}
}
The above is basically what's included in the tutorial .zip file.
Upvotes: 1
Views: 28612
Reputation: 21
This works for me..
just allow tcp connection to the port,
use the below command sudo ufw allow 5000/tcp
here i used port 5000,
change it according to your port number.
Reference : https://blog.cloudboost.io/deploying-a-node-js-express-application-on-digital-ocean-part-1-5d5b0cfe0a34
Upvotes: 2
Reputation: 947
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.
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.
When you say:
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.
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.
Finally, it should be mentioned, that you should never, ever expose sensitive information on the internet. Delete the public ip address from your question.
Upvotes: 4
Reputation: 36063
When you simply use app.listen(port)
, it will only allow connections from localhost (as you are seeing).
To permit external connections, use app.listen(port, "0.0.0.0")
. This tells Node to listen on the external network interfaces.
Also, make sure your EC2 instance's security group is allowing incoming connections on the appropriate port.
Update:
As pointed out by jfriend00, and further investigation, this is not correct.
Ref: http://expressjs.com/api.html#app.listen
Upvotes: 5