3

I am new at node js and watching tutorials. But I am confused a bit about deploy node application.

var http = require('http');

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

console.log('Server running at http://127.0.0.1:1337/');

This is a server file code. I am running with this command: % node example.js

This is Working on console...

But other platforms contains management area, (Php, IIS, Tomcat). www folder includes application files. Running service background. We change code and save it, but not restart service.

we specify everything on js file at node js platform. Run it from console. I could not understand running and deploying logic.

If I have linux server, or windows server, Should I open terminal and run application for each application? If I close terminal my application will stop?

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • 1
    node works differently because the server is created within the application. You will need to restart the application which in turn restarts the server – zgr024 Jan 09 '15 at 18:12
  • NodeJS application server similarily to Python/Ruby/etc needs to be restarted on changes. Take a look at this question: http://stackoverflow.com/questions/1972242/auto-reload-of-files-in-node-js – Andrew Dunai Jan 09 '15 at 18:20

1 Answers1

8

In a classical PHP setup, the web server is separate from the application. The setup looks like this:

[browser/client] => [apache/mod_php] => [index.php]

With node, things are different, because the web server is part of the application. So your setup looks like this:

[browser/client] => [node server.js]

So, what does that mean for deployment?

Usually it means, you need a supervisor that starts your application and restarts it if it crashes. When you copied over a new version of your application, you simply use the restart mechanism of your supervisor.

Some supervisors even restart automatically when they notice the application's code changed, which is similar to PHP's change-and-reload workflow.

A small selection of supervisors you could use follows:

But there are many alternatives.

If you'd start your application from the terminal on the server, it would normally only run until you terminate the terminal session. When the server restarts (maybe because of a power or hardware failure), you have to restart your application manually. Because of that, the supervisor should be

  • Windows: configured as a service (Auto start node.js server on boot)
  • Linux: I would simply install supervisord using the Linux distribution's package management and configure it to start the node application. Alternatively, you can hook up to the distribution's init system (create an init script). Different distributions often have different init systems.

Additionally, if you need

  • more than one application running on one server, even node and PHP
  • need some of the built-in behaviour of most webservers, like serving static content, caching, gzip, rate limiting, SSL termination etc.

you most certainly need a reverse proxy in between your applications and the clients.

The setup would look like this:

                                   /=> [apache/mod_php] => [index.php]
[browser/client] => [reverse proxy] => [node server1.js]
                                   \=> [node server2.js]

Most web servers can also be configured to act like a reverse proxy. There are also specialized reverse proxies.

stefreak
  • 1,460
  • 12
  • 30
  • I edited question: Should I open terminal and run application for each node application? – barteloma Jan 09 '15 at 18:45
  • I would express things slightly differently. The node process is running an *application* server. There's still a lot of advantage in having a web server (nginx or apache) in the architecture. You would then run your node process as a service (as you described) on a private port (e.g. 3000) and then have apache or nginx act as a reverse proxy, forwarding certain requests to the running node app. – Paul Jan 09 '15 at 18:53
  • yeah I thought of mentioning that... maybe I should, thanks :) – stefreak Jan 09 '15 at 18:53
  • 1
    Good point on the multiple applications. I was thinking just to take advantage of out-of-the-box functionality like static asset caching and so on. – Paul Jan 09 '15 at 19:02