Search code examples
javascriptapachenode.jsubuntu-server

Running node.js on a port other than port 80


I have an apache web server running on my ubuntu server box. Recently, I was attempting to learn JavaScript and I stumbled upon node.js. I'd like to create a few web application multiplayer games and I've learnt that node.js could come in handy. I was experiencing a few issues with the configuration. How would I go about running both apache server and node.js on the same machine? I don't mind it if the applications on node.js are on a different port and have to be accessed by typing in websitename:portNumber. I am not too concerned about the performance advantages/disadvantages, I'm just like to take the opportunity to try out JavaScript and node.js. Are there any files which have to be modified?

Here's the code I have for the script running on the server (only using it for trial purposes for now):

var http = require('http');

 http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
  }).listen(1337);

I started the server (node fileName.js). However, When I try to access it with another client computer on the network, it doesn't seem to be working as the page doesn't seem to exist.

What is the procedure so that I can get Hello World outputed to my browser when I visit the server on port 1337?


Solution

  • Try to do it like this:

    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(1337, "your-adapter-ip"); // don't forget adapter IP
    

    And then from the network point your browser to http://your-adapter-ip:1337.
    Make sure that the firewall is open on that port.