Back-end applications with node.js

4

I've seen a few lectures on Node.js , its capacity, usability, market, etc ... But all of this information got really "scrambled" for me, about him.

What I know, correct me if I'm wrong, is that it works like server , very similar to Apache , however done with javascript, which runs with javascript.

My main question is how to work with Back-end applications with this tool. What exactly can I create? How can I create? Where can I create? And how does your interaction with servers like Wamp work?

In several examples I see codes like this:

var http = require('http');
http.createServer(function(req,res) {
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); 
  res.end('Olá mundo!');
}).listen(3000);
console.log('Servidor iniciado em localhost:3000. Ctrl+C para encerrar…');

A Hello world! in node. How dynamic and interactive can this be?

Back-end is not with me, so much so that I'm wondering if the title of the question is redundant ... rsrs. So how much more didactic could be with this theme, thank you! :)

    
asked by anonymous 11.01.2016 / 23:11

1 answer

7
  

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 will answer if it is useful for others.

*: How do you say "disclaimer" in Portuguese?

For me the biggest advantage is having JavaScript on both client / server side. So I can use the same library, same objects and have better control over the code.

The big advantages I've heard about Node are asynchronous processing, which is very powerful for what I've been working on.

The server on the Node works by itself. It may be advantageous to have Nginx also run as a traffic manager and serve static cache content. There is an interesting video about this possibility here

Using Nginx, setup may look 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 site I was working with was the current MooTools site . Some interesting parts of the site that are good examples for your question:

Integration with twitter , compiling the online framework code and sending immediate files to the client, a very efficient routes through the Express API, another example where we compiled the entire library documentation (about 500 pages) with asyncrona / parallel reading and compiling of files and then Caches waiting to be requested , and recompile in case of moving files in the directory, etc.

Node.js is a platform, a code interpreter, that makes it possible to use applications written in JavaScript on the server side. In the Node platform can be done basically the same as in other platforms, with complexity, processing capacity. The Node works with MySQL, MongoDB, SQLite etc etc.

An example of simple application:

There is a heavily 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 package.json which is a JSON file where application dependencies are stored, with reference to which version of the package / module to use. 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 for starting an application, but this implies that it is defined in package.json . If not, ie alternatively, node nomedaaplicacao can be used and the application is running on the 3000 port, which is the default port. Opening the browser with 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 items. HTML and CSS precompilers, particularly the ones I use most often: Jade and Stylus . These deserve to be explained apart, but are an essential tool for code organization and the development process.

    
12.01.2016 / 00:52