Introduction to NodeJS + SailsJS

Hello to the whole community! My name is Bruno, and I've been in DesdeLinux as a user. It's time for me to make my first posts in this community 🙂

Today I bring you a guide on NodeJS y sails.js

Node.js

It is a programming environment, based on the language javascript with an event-oriented architecture, ideal for asynchronous programming. Node, is based on the engine V8 of Google.
It maintains an active open source community, who have written many modules to facilitate our development.
One is socket.io (Allows the server to send real-time updates to the client, for example a GPS tracking)

What is?

Node is designed for situations where the traffic is too large, causing server logic and throughput not necessarily higher. And not only that, but in real time and with great scalability!

Why Node?

1) It is open source.
2) If you ever thought of using a single language on both server and client side, here's the solution.
3) Scalability and Concurrence.
4) etc

sails.js

I chose sails, for the following characteristics in addition to that in my point of view it was the most readable in terms of code. Eye! exist MANY frameworks for Node, as it is Express, Angular, geddy, etc.

Screenshot - 080913 - 13:21:50

What is Sails?

It's a MVC framework that mimics the model of ruby on rails (Yes, you heard correctly: D) for the creation of small or large applications quickly, easily and safely. further has support for modern application requirements. It doesn't matter which database you use, Sails provides a layer of abstraction, which makes the choice of it indifferent. Sails has:

- The ability to create RESTful JSON API automatically.
- It has incorporated the module socket.io.
- Generate routes automatic for your controllers.
- Provides system of authentication of users and access control role-based.
- Grunt as Task Runner (Automatic tasks such as minification, compilation, testing, etc)
- Assets: All files in their corresponding directories (css, js) are unified in a single file and minified, to reduce considerably page load and limited number of browser requests.

Installation of Node.Js

The turorial I did with Ubuntu 13.04.

sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Sails.Js installation

Latest stable version:
sudo npm -g install sails

At this point without errors, we create a new project! 😀

Create the directory skeleton:
sails new testProject

We enter our Project:
cd testProject

We start the server:
sails lift

Yeeeeeeeeeeah! we entered http://localhost:1337/

Screenshot - 080913 - 13:02:34

The structure of our directory should look like this:
Screenshot - 080913 - 13:07:57

FIRE:
- Drivers
- Adapters
- Models
- Policies
- Services

ASSETS:
- images
- js
- styles
favicon
robots

SETTINGS:
- local
404.js, 500.js… ..
routes.js
session.js
sockets.js …… ..

NODE_MODULES:
- ejs (template engine)
- grunt
- optimist
- sails-disk

VIEWS:
- Home (here is the root template, localhost)
- 404.ejs
- 500.ejs
- layout.ejs (For those who used rails, layouts.ejs behaves same than layouts.html. *)

EJS (Embedded Javascript) It is a template engine, but not the only one. I particularly like JADE better

END

So far my knowledge, and I hope I have been of help to many ... I am new in this technology so any constructive criticism is welcome : )

This post arose to do it, because I am developing a kind of psychosocial network (Nothing from another world). With purposes of learning, and few resources. the idea is to also make a home server that supports it (Surely make a tutorial of it) As I said before, the resources are few, so the optimization It's very important. (and even if they weren't too ^^)


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Arthur shelby said

    I'm amazed at how faithful it emulates RoR, although many hate RoR for its poor performance (without configuring) trendsetting code cleaning.

    The subject is very interesting, I hope you continue to develop it, congratulations.

    1.    dwarf said

      I don't hate RoR, the truth is that ruby's philosophy of "there are many ways to do the same thing" is usually a problem because not all of them are really the most efficient and when you want to answer a question, in many cases you have that Little problem in which everyone responds with a different way of doing the same and you tend to get more confused D:

      Regarding this framework for Node, as it is not for huge or large things, Node is used together with other frames such as Django for specific things, I think the example of Mejorando.la is like this, they build the whole site and manage its bases of data with Django but the realtime issues are left to Node, though I'm not sure so I can't talk too much about that.

      1.    jamin samuel said

        I think that's how you say, they use Django for the base of the site and Node for things in real time ... Well at least that's what I always hear freddy vega say

        1.    Bruno cascio said

          NodeJS is not like Django or PHP, or ruby. But it is a server such as Apache, Nginx, etc.

          Sails is the framework that makes use of nodejs and express (another framework) together with some modules such as socket.io, for the creation of web applications.

          Regards!

          1.    Arnoldo RLF said

            so if you want to include a chat made with nodejs with a page in python with Django (to continue with the example of improving.la), you would have to use a frame, or assign to apache (if that is the case, I have no idea which server use python) the path http://miweb.com ya node.js http://miweb.com/chat and communicate them

  2.   jamin samuel said

    Hey that super interesting….

    What I did not finish is why is Node JS used for the web?

    1.    Arnoldo RLF said

      it is an alternative to apache, with the advantage of being in real time (applications that are communicating all the time and not only when the request is made).

      1.    David Gómez said

        NodeJS is not an alternative (or replacement) to Apache, NGINX or any other web server. NodeJS is a JavaScript interpreter for the server, allowing you to create applications with this language that would otherwise require the use of languages ​​such as Java, Python, Ruby or PHP.

        1.    Bruno cascio said

          Yes, maybe I expressed myself wrong. What I wanted to convey is that node.js runs its own server and does not need 3rd parties to work (apache, nginx, cherokee, etc)

          Regards!

  3.   Arnoldo RLF said

    very good, I hope more tutorials to be able to follow node.js and do something in real time

    1.    Bruno cascio said

      Here is a 200-page book, with an introduction to Node.js through Koans. It has an into to Node.js, and the "resolution" of a part of a problem posed (the example is twitter) with Express as the header framework… ..

      http://nodejskoans.com/

      PS: Sails is made on an Express basis, usually most of them are.

      Regards!

  4.   elav said

    Excellent contribution Bruno .. Welcome 😀

    1.    Bruno cascio said

      Thanks to you and the whole community for letting be part of it 🙂 Greetings!

  5.   auroszx said

    Very interesting Bruno 🙂

    1.    Bruno cascio said

      Thank you!

  6.   The sponge woman said

    Excellent article. Thanks a lot.

    I currently use sails in some of my projects. I would like to ask you where I can get more information about the role-based access control of sails, if possible in Spanish, but if not, in English. I would like to add access control to one of my pages but I want to establish permissions for some users from a certain route and others from another route. Is that possible with sails? I can't find much information about it.

    Thanks and congratulations for the article.

    All the best
    Elvira.

    1.    Bruno cascio said

      Hello Elvira!

      Regarding what you mention, be it Sailsjs or any other framework, what you propose is related to the modeling of the data and the restrictions that the system imposes.

      You could create something like "groups" (roles) with different permissions and associate each user to one of them.

      Particularly in sails they are called "policies" and you can see it here: http://sailsjs.org/#/documentation/concepts/Policies

      Greetings and thanks for your comment!

      1.    The sponge woman said

        Thank you very much Bruno. With your prompts, I found an elegant solution. a greeting