Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to work with node js like apache or iis

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?

like image 830
barteloma Avatar asked Dec 02 '25 04:12

barteloma


1 Answers

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:

  • https://github.com/remy/nodemon
  • https://github.com/isaacs/node-supervisor
  • http://supervisord.org/

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.

like image 135
stefreak Avatar answered Dec 03 '25 19:12

stefreak