ドキュメント

Node.js

Node.js is increasingly gaining popularity among Web developers. However, due to lack of hosting offers at the moment, Node.js remains the prerogative of dedicated servers and VPS. Actually, the last is affordable even for a small project :)

More documentation you can find at nodejs.org.

As you know, Node.js supports packages (modules). Global packages folder is \winginx\nodejs\node_modules\.

Node.js Package Manager NPM, popular Node.js web framework Express.js and template engine Jade are included.

Debug Node.js script through Node-Inspector. You can access it at http://localhost:8080/ (it needs only webkit compatible browser, like, Google Chrome).

To work with databases packages MongoDB and MySQL are included, too.

In short we can say that Node.js, in contrast to PHP, is not interpreted, and compiled as a standalone server. In conjunction with nginx it works as a backend-server, practicing dynamic queries, and nginx is engaged in statics.

An example:

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/');

Save this script in home/nodejs.local/server.js.

Here there is a sample config for nginx:

server {
  listen       127.0.0.1:80;
  server_name  nodejs.local www.nodejs.local;

  root   home/nodejs.local/public_html;
  index  index.html;

  log_not_found off;
  charset utf-8;

  location / {
    if (!-e $request_filename) {
      proxy_pass http://localhost:1337;
    }
  }
}

Pay attention to the proxy port, it must be equal to the port Node.js script is listening to.

It remains to tell Winginx that we want to run our server on Node.js. You can make it using Winginx TM or manually add a line to the config nodejs\node.conf:

server = home/nodejs.local/server.js

After Winginx has restarted, you can call it at http://nodejs.local/. (Don't forget to add nodejs.local domain to the hosts file).

Can Node.js work together with PHP in one website?

Yes, it can! To do this you need to determine what is for PHP, and what is for Node.js.

This can be a special URL for Node.js service. For example, we want whole website to be in PHP, but a chat to be served by Node.js at http://site.local/chat/. So, we need to create a location /chat/ and pass the process to Node.js. See a sample config

Winginx © Alexei Shabalin, 2011-2023