Disclaimer*: I think this question is at the limit of off-topic but as Node.js seems to be little used here in Sopt I answer if it is useful for others.
*:how do you say "Disclaimer" in English?
For me the biggest advantage is to have Javascript on both client/server sides. So I can use the same library, identical objects and have better control over the code.
The great advantages I have heard about on Node are the asynchronous processing, which is very potent for what I have been working on.
The server on the Node works on its own. It may be advantageous to have Nginx running also as a traffic manager and serving static cache content. There is an interesting video about this possibility here (in English)
When with Nginx the setup can be like this:
upstream project {
server localhost:3000;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
proxy_pass http://project;
}
}
The first website I worked on was the current website of Mootools. Some interesting parts of the site and that are good examples for your question:
Integration with twitter, compilation of online framework code and immediate sending of files to the customer, a Very efficient Routes through the Express framework API, another example where we compile all the library documentation (about 500 pages) with asyncronous/parallel file reading and compilation and then cached waiting to be requested, and recompile in case of file changes on the board, etc.
Node.js is a platform, a code interpreter, that makes it possible to use applications written in Javascript on the server side. On the Node platform you can do basically the same thing as on other platforms, with complexity, processing capacity. Node works with Mysql, Mongodb, Sqlite etc etc.
A simple application example:
There is a widely used framework that runs on top of the Node called Express. This framework has a very useful application pattern generator. With some commands the application is running:
Running two commands generates an application:
$ npm install express-generator -g
$ express myapp
create : myapp
create : myapp/package.json
create : myapp/app.js
create : myapp/public
create : myapp/public/javascripts
create : myapp/public/images
create : myapp/routes
create : myapp/routes/index.js
create : myapp/routes/users.js
create : myapp/public/stylesheets
create : myapp/public/stylesheets/style.css
create : myapp/views
create : myapp/views/index.jade
create : myapp/views/layout.jade
create : myapp/views/error.jade
create : myapp/bin
create : myapp/bin/www
Node.js uses the package.json
which is a JSON file where application dependencies are stored, with reference to which version of the package/module should be used. To install everything at once just do:
$ cd myapp
$ npm install
And to run the application just run:
$ npm start
Note that npm start
is the default command to start an application, but this implies that this is in the package.json
. If not, ie alternatively, it may be used node nomedaaplicacao
and the app is running at the door 3000
, which is the default port. Opening the browser with the url: http://localhost:3000/
the app is running.
The structure of this board will be:
.
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
That is: very simple.
To use other libraries just install:
$ npm install --save # --save é para guardar no package.json a informação da dependência
and then inside the code:
var mysql = require('mysql');
There are still two very interesting factore. HTML and CSS pre-compilers, namely the ones I use most often: Jade and Stylus. These deserve to be explained separately, but are an essential tool for code organization and development process.
Sergio, a doubt, the
localhost
is what we crack with thewamp
, For example, or does he install it? For example, if I don’t have a server installed, i.e., I don’t have a localhost, when following these steps, Node.js creates a server?– Samir Braga
@Samirbraga Node is an HTTP server, it no longer installs anything like Mysql, Nginx, etc. It is possible to choose the port it "listens to" but the rest of what we call "server" in a general term Node does not install.
– Sergio
Today I saw several things about Ode. I could see the coherence of your reply @Sergio. I want to go deeper into this... :)
– Samir Braga