Back-end applications with Node.js

Asked

Viewed 4,084 times

3

I’ve seen some lectures on the Node.js, its capacity, usability, market, etc... But all this information was very "scrambled" for me, and I always had a somewhat vague thought about it.

What I know, correct me if I’m wrong, is that it works like a server, similar to the Apache, but done with javascript, which runs with javascript.

My main question is how work with applications Back-end 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?

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 this can be?

Obs.: Back-end It’s not up to me, so much so that I’m in doubt whether the title of the question is redundant...rsrs. So the more didactic you can be with this theme, I thank you! :)

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 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 the wamp, 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?

  • 1

    @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.

  • 1

    Today I saw several things about Ode. I could see the coherence of your reply @Sergio. I want to go deeper into this... :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.