node.js is server-side asynchronous Javascript, an excellent tool for creating server-side COMET applications, in particular, for toys, chats, and other high-loaded projects, using Javascript syntax precompiled into machine code running at a speed comparable to C ++ code, is very fast, able to keep 10 - 12 thousand connections, and not creating a separate process / not interpreting itself again / not starting the process with each new client. Uses as a basis the V8 - Javascript engine from Google. Convenient features, you can set the value of the variable with one visitor, and read this value when responding to another.
I draw your attention to the fact that if you have not programmed, for example, in Python or Ruby, only in PHP, do not bypass it, this is not another unfamiliar language. And this is its advantage - Javascript is a familiar language, especially for those who programmed in PHP + Javascript, but PHP did not allow so many things, especially in conjunction with Apache, for example, download files with a download bar (without Flash), the possibility of low-level network management (the ability to write, for example, a client or a Mysql server or a proxy server like nginx).
The flexibility of the language is indicated by the fact that extensions for working with MySQL, for example, are written in Javascript itself.
I planned to write an article describing this wonderful system, with examples, but at the time of reading it would be good for the reader to have the installed version of node.js on hand. Therefore, this post is about how to install node.js, including on Windows, which is important in connection with the recent appearance of the port under cygwin. (Yes, there is information about where to download node.exe)
Important note.
First of all I want to draw attention to the fact that with the development of node.js, its syntax has changed, sometimes several times. In this regard, I categorically do not recommend using old versions of the node, recently slipping node.NET, and similar assemblies. Ideally - version 0.1.90 or newer. For the same reason, be careful when you find installation tutorials on the network. Those that start with wget
http: //s3.amazonaws ... will download the outdated version.
Before installing, make sure that python is installed, it is needed for configuration and build using make (relevant for everything except Windows in some cases).
GNU / Linux
Under Linux node.js is the easiest to install.
We go to
nodejs.orgWe copy the link to source codes
wget http://nodejs.org/dist/node-v0.1.97.tar.gz
tar -xvf node-v0.1.97.tar.gz
//
git clone git://github.com/ry/node.git ( git )
cd node*
./configure
make
make install
Habrahabr advises in comments:
ProgrammerIt would be better to build at least using checkinstall
checkinstall --fstrans=no --install=no --pkgname=node.js --pkgversion "0.1.97" --default
The logic suggests that under MacOS the installation will be the same, I can not check. Developers are testing their offspring on MacOS, it should work without problems.
Freebsd
I have VDS on FreeBSD, and therefore this system is the most relevant for me. However, the developers do not test node.js on FreeBSD, so some problems are possible.
Before installing, make sure that you have libexecinfo. It is put like this:
cd /usr/ports/devel/libexecinfo
make install
Also prompted
hilobok :
On FreeBSD, in order not to collect libexecinfo from ports, it is enough to do:
pkg_add -r libexecinfo
So, the classic way # 1
cd /usr/ports/www/node
make
make install
The one that the doctor prescribed is recommended for everyone. Those who did not know that node is present in the ports - know.
Method # 2
If node ports are absent, you can try to install using the classic method described above (under Linux): wget or git clone; tar -xvf; configure; make etc.
But, apparently, this way it will not work to install the node on a 64-bit system.
Method # 3
If you have any problems (and I have), there is another way.
Go to
www.freebsd.org/cgi/query-pr.cgi?pr=145641Download the node-0.1.90.shar file
Or create a node-0.1.90.shar file and fill its contents manually (for those who have wget stumbles on an ampersand sign in the url, and you don’t want to figure out what's wrong).
We speak:
sh node-0.1.90.shar
cd node
make
make install
Similar files (.shar), if they appear later, can be found here:
b23.ru/exxwWhen building under FreeBSD 8.0_RELEASE, amd64 was not collected from the first time:
...obj/release/cpu-profiler.o(.text._ZN2v88internal23ProfilerEventsProcessor19FunctionCreateEventEPhS2_i+0x81): In function `v8::internal::ProfilerEventsProcessor::FunctionCreateEvent(unsigned char*, unsigned char*, int)':
: undefined reference to `v8::internal::OS::ReleaseStore(long volatile*, long)'
obj/release/cpu-profiler.o(.text._ZN2v88internal23ProfilerEventsProcessor15CodeCreateEventENS0_6Logger16LogEventsAndTagsEiPhj+0xa3): more undefined references to `v8::internal::OS::ReleaseStore(long volatile*, long)' follow
scons: *** [obj/release/mksnapshot] Error 1
scons: building terminated because of errors.
Waf: Leaving directory `/home/atercattus/node.js/node-v0.1.97/build'
Build failed: -> task failed (err #2):
{task: libv8.a SConstruct -> libv8.a}
*** Error code 1
This link helped: code.google.com/p/v8/issues/detail?id=726 .
Everything is simple there, in v8 / src / platform-freebsd.cc is added between int OS :: ActivationFrameAlignment () and const char * OS :: LocalTimezone (double time) implementation of the missing function:
void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
__asm__ __volatile__(""::: «memory»);
*ptr = value;
}
Maybe someone will help.
Update on June 11th
The above patch is not relevant for the latest version of git - it compiles perfectly under freebsd-amd64
')
Installation under Windows
Forget about the .NET version, at least until the port of version 0.1.90 appears (see note above). We'll have to use cygwin. There are two ways - find, download, install Cygwin, add python, g ++, other dependencies, perhaps handle the source node ... If you have time and desire, please, in this case, take a Cygwin-compatible port
here .
I offer a simpler version:
Download archive with precompiled node.exe:
drop.io/2dwcadi or
dl.dropbox.com/u/626643/node-cygwinx86.zip - a mirror on the dropbox. This is the latest version available for Cygwin (0.1.95), besides, cygwin is not needed for it (it is portable). Unpack.
There is another fallback through colinux:
bit.ly/9hCjHONote:
azproduction :
For windows users (if you have node.exe and server.js are in different places), the command to run from under the Cygwin version will be:
node.exe /cygdrive/_////server.js
relative paths do not understand, windows paths with any slashes also do not understand.
Also a small update - at the moment (June 11), from under cygwin, you can build the original node.js by downloading it with git.
Test Drive
To start, create a file, for example, server.js. Sample content can be taken at
nodejs.org .
In cmd, go to the unpacked folder and say:
node.exe server.js
In * nix it is similar, go to the unpacked folder and say:
node server.js
I draw your attention to the fact that the brake in two seconds in the example is not a brake, but a special delay in return, realized through setTimeout, simply as a demonstration of the possibilities.
My favorite example is a bit different:
var sys = require('sys'),
http = require('http');
var i=0;
http.createServer(function (req, res) {
i++;
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World'+i+'\n');
res.end();
}).listen(8000);
sys.puts('Server running at 127.0.0.1:8000/');
Note the increment of the variable when refreshing the page.
To shut down the server / restart with another script, press Ctrl + C (and rerun node). Note that if you run node through putty, for example, then when you close the console, the node will turn off. I think it’s easy to figure it out, the easiest option is to start the screen, or to demonize the process by adding a node to /etc/init.d, turning it off using the stop or killall node command. More information about this installation (in English)
here .
It's all.
Nginx configuration
Next, your server will most likely work on port 80, and node, for example, on 8000. To make it easier to write scripts with beautiful addresses, configure nginx, writing in the config file:
location /ajax/ {
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
As noted in the comments, this design is not suitable for real projects, and you need to customize Timeout, message sizes, etc. In addition, you still have to adjust the number of simultaneous connections in the system itself. That is, this design is suitable for review and for testing.
Now, when we request
example.com/ajax, the content will return node.js.
Note that the script launched in node in this case will determine the url not as equal to '/ ajax /', namely as '/'.
You can stop at this by getting a working node.js, you can begin to touch it with pens. This thing is really wonderful. Finally a few links:
nodejs.org/api.html Documentation
kuroikaze85.wordpress.com/all-node-js-entries - Collection of articles on the site habracheloveka Sergei Shirokov (
kurokikaze ).
howtonode.org - articles and notes in English by Tim Caswell (Tim Caswell aka creationix).