2

I have Apache also running on my machine, I have to run my application without adding a port number.

It works when I access it from http://localhost:2121 using the following:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('hello');
}).listen(2121);
console.log('Server running');

How do I set it to use http://localhost (without the port number at the end.)

vikrant
  • 141
  • 1
  • 12

2 Answers2

2

Apache is occupying Port 80 for you. If you try to start your node server with port 80 (assuming your apache is up) you will get a permission error. The right approach is to reverse proxy your node app and serve it via apache. Your apache config should look like the following.

         <VirtualHost *:80>
           ServerName localhost
           ServerAlias localhost
           DocumentRoot /path/to/your/node/app
           Options -Indexes
           ProxyRequests on
           ProxyPass / http://localhost:2121/
         </VirtualHost>

Also, my advise to you is to use Nginx if possible, makes life a lot easier...

irimawi
  • 368
  • 1
  • 9
0

By default http port runs on 80 port so if you do like this

`

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('hello');
}).listen(80);
console.log('Server running');

`

You will be able to access your content at http://localhost/

Also remember you cannot run more than one application on one port . It will not work.

Kartikeya Sharma
  • 553
  • 1
  • 5
  • 22