jeudi 15 octobre 2015

How to make NodeJS Server deployed on AWS cloud go live?

I am new to NodeJS. I have the following code for hello world.

var http = require("http");
http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World!\n I am the AWS cloud');
    }).listen(8081);
console.log('Server is running at http://127.0.0.1:8081');

This code is saved on my AWS cloud which runs an Ubuntu 14.04 instance. I have installed nodeJS on Ubuntu. When I run the code it works fine and displays the console log message, but when I open a web browser on a different machine and type http://Public-IP-Address:8081 it does not work.

Also when I stop the script by pressing Ctrl+c and then again execute it, it displays the following error message:

Server is running at http://127.0.0.1:8081
events.js:85
      throw er; // Unhandled 'error' event
        ^
Error: listen EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at Server._listen2 (net.js:1156:14)
    at listen (net.js:1182:10)
    at Server.listen (net.js:1267:5)
    at Object.<anonymous> (/home/ubuntu/NodeJSscripts/hw.js:17:22)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

When I go back and change the port number and run the script again, it then runs again without throwing any error, but the web browser still doesn't connect to the server through it's public IP.




1 commentaire: