M
M
moon_and_altair2014-11-07 16:07:18
Node.js
moon_and_altair, 2014-11-07 16:07:18

Run hello world on nodejs?

I do the following code on the server:

var n = 80;

var http = require('http');
http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n'+n);
}).listen(n);
console.log('Server running at '+n);

I run node file.js and writes in response:
Server running at 80

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/var/wwwdomen/data/www/domen.ru/file.js:7:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Accordingly, when starting the site, I see Index of /
If I make a different port, for example 8080:
var n = 8080;

var http = require('http');
http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n'+n);
}).listen(n);
console.log('Server running at '+n);

Writes:
Server running at 8080
and hello word starts when you enter anyDomainAtServer:8080
I need a specific domain, and so that I don’t have to write any:ports.
How to do it right?
PS Apache/2.2.16 (Debian)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2014-11-07
@Sanasol

In the first option, the port is busy and it throws an exception that you should catch somewhere, but you don't.
To start on a specific domain, either use a dedicated IP address for the domain, or proxy through nginx. Couldn't find any other solutions by quick googling.
Turn off Apache and start the server on port 80. (or turn on nginx and proxy to node)

V
vsvladimir, 2014-11-07
@vsvladimir

As an option in Apache, you can redirect port 80 to the desired domain for the desired domain. For example node server is running on port 3000:

<VirtualHost node.example.com:80>   
    ServerName node.example.com  
    ProxyPreserveHost On
    ProxyPass / http://localhost:3000/
</VirtualHost>

In Apache, you must first connect the proxy modules:
a2enmod proxy
a2enmod proxy_http

F
Fadi Haj, 2014-11-10
@deleted-fdhaj

var http = require('http'),
    host = '127.0.0.1',
    port = 8080;
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port, host);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question