Install node.js on Linux, FreeBSD, Windows

    node.js - server-side asynchronous Javascript, an excellent tool for creating the server side of COMET applications, in particular, for toys, chats, and other highly loaded projects, using Javascript syntax, compiled into machine code, running at a speed comparable to C ++ code, is very fast, able to hold 10 - 12 thousand connections, and not creating a separate process / not interpreting itself again / not starting the process with each new client. Uses V8 as the basis - Google’s Javascript engine. Convenient features, you can set the value of a variable at one visitor, and consider this value when answering 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, the upload of 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 evidenced 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 nice for the reader to have an installed version of node.js. Therefore, this post is about how to install node.js, including on Windows, which is relevant in connection with the recent appearance of the port under cygwin. (Yes, there is information on 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 strongly do not recommend using old versions of node, recently slipping node.NET, and similar assemblies. Ideal - version 0.1.90 or later. For the same reason, be careful when finding installation tutorials on the network. Those starting with wget http: //s3.amazonaws ... will download the legacy version.
    Before installing, make sure that python is installed, it is needed for configuration and assembly using make (relevant for everything except Windows in some cases).

    GNU / Linux

    Under Linux, node.js is the easiest to install.
    We go to
    Copy the link to the sources. Habrahabr advises in comments: Programmer
    tar -xvf node-v0.1.97.tar.gz
    //либо делаем так
    git clone git:// (если git есть)
    cd node*
    make install

    It would be better to build at least with checkinstall
    checkinstall --fstrans=no --install=no --pkgname=node.js --pkgversion "0.1.97" --default

    Logic suggests that under MacOS the installation will be similar, I can’t check. Developers are testing their brainchild on MacOS, it should work without problems.


    I have VDS on FreeBSD, and therefore for me this system is the most relevant. However, developers do not test node.js on FreeBSD, so some problems are possible.
    Before installing, make sure libexecinfo is installed. It is put like this: They also prompted hilobok :
    cd /usr/ports/devel/libexecinfo
    make install

    On FreeBSD, to avoid compiling libexecinfo from the ports, just do:
    pkg_add -r libexecinfo

    So classic way # 1
    cd /usr/ports/www/node
    make install

    The one that the doctor prescribed is recommended for everyone. For those who did not know that node is present in ports - know.
    Method # 2
    If the ports have no node, you can try to install using the classical method described above (under Linux): wget or git clone; tar -xvf; configure; make, etc.
    But, apparently, in this way it will not work to install node on a 64-bit system.
    Method # 3
    If you have any problems (and I have), there is another way.
    We go to the page
    Download the file node-0.1.90.shar
    Or create the file node-0.1.90.shar and fill in its contents manually (for those with wget will stumble over the ampersand sign in the url, and I don’t want to understand what’s the matter).
    We say : Similar files (.shar), if they appear later, can be found here:
    sh node-0.1.90.shar
    cd node
    make install

    The method of AterCattus
    When building under FreeBSD 8.0_RELEASE amd64 the first time it did not come together: This link helped: . Everything is simple there, in v8 / src / it is added between int OS :: ActivationFrameAlignment () and const char * OS :: LocalTimezone (double time) implementation of the missing function: Maybe someone will help.
    ...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

    void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
    __asm__ __volatile__(""::: «memory»);
    *ptr = value;

    June 11 update
    The above patch is not relevant for the latest version from git - it compiles fine under freebsd-amd64

    Windows installation

    Forget the .NET version, at least until the port version 0.1.90 appears (see note above). I have to use Cygwin. There are two ways: find, download, install Cygwin, add python, g ++, other dependencies, maybe edit the source of the node handles ... If you have the time and desire, please take a Cygwin-compatible port here .
    I suggest a simpler option:
    Download the archive with the precompiled node.exe: or - a mirror on dropbox. This is the latest version available for Cygwin (0.1.95), in addition, cygwin is not needed for it (it is portable). Unpack.
    There is another fallback through colinux:
    azproduction :
    For windows users (if you have node.exe and server.js in different places), the command to run from under the Cygwin version will be: it
    node.exe /cygdrive/буква_диска/путь/до/скрипта/server.js
    does not understand the relative paths, it also does not understand the paths with any slashes.

    Also a small update - at the moment (June 11), from cygwin you can collect the original node.js by downloading it using git.

    Test Drive

    To start, create a file, for example, server.js. Sample content can be taken at .
    In cmd, we go to the unpacked folder and say:
    node.exe server.js
    In * nix the same way, we go to the unpacked folder and say:
    node server.js
    I draw attention to the fact that the two-second brake in the example is not a brake, but a special delay in return implemented through setTimeout, just as a demonstration of the possibilities .
    My favorite example is a little different: Pay attention to the increment of the variable when refreshing the page.
    var sys = require('sys'),
    http = require('http');
    var i=0;
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Hello World'+i+'\n');
    sys.puts('Server running at');

    To shut down the server / reboot with another script, press Ctrl + C (and restart node). Note that if you run node through putty, for example, then when you close the console, node will shut down. I think it’s easy to figure it out, the easiest option is to start screen, or to demonize the process by adding node to /etc/init.d, turning it off with the stop or killall node command. Read more about such an installation (in English) here .
    It's all.

    Nginx setup

    Further, your server will most likely work on port 80, and node, for example, on 8000. To make it more convenient to write scripts with beautiful addresses, configure nginx, writing in the config: As noted in the comments, this design is not suitable for real projects , and you need to configure Timeout, message sizes, etc. in addition, you still have to configure the number of simultaneous connections in the system itself. That is, this design is suitable for review and testing. Now when requesting, the contents will return node.js. Please note that the script running in node in this case will determine the url not as equal to '/ ajax /', but rather as '/'.
    location /ajax/ {
    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;

    You can stop here, getting a working node.js, you can start touching it with pens. This thing is really wonderful. Finally, a few links: Documentation - A collection of articles about the node of the harachelove Sergei Shirokov ( kurokikaze ). - articles and notes in English by Tim Caswell (Tim Caswell aka creationix).

    Also popular now: