I hear a lot of people who ask What’s the differences between Sails and Trails ? or Why should I use Trails instead of Sails ? so I decide to explain my vision and things I have seen between these two Node.js frameworks.
I will not tell you one is better than the other, I will let you judge about this because it will depend of your needs and philosophy.

Definition

Here are definitions taking from officials github repos :

Sails : Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It’s especially good for building realtime features like chat.

Trails : Trails is a modern, community-driven web application framework for node.js. It builds on the pedigree of Rails and Grails to accelerate development by adhering to a straightforward, convention-based, API-driven design philosophy.

I will add that Trails is not a fork of Sails, in fact 0 line of code come from Sails. Trails was full rewrite and is a new fresh framework.

I will not keep from you that these two frameworks have the same functionalities, they are :
* Generate automatic REST API from models
* MVC structure
* Support websockets for real time
* Support multiple databases
* Have plugin system
* Have policies to secure
* Front-end agnostic
* 100% JS

Differences

1) File loading

Sails

When you start using Sails, it’s magic you put your files under api and start your server and Sails load them automatically. It also loads config automatically. That’s cool for developing a prototype quickly, but loading all these file take time and can slow down server start (yes #perfmatters ^^).

Trails

In Trails it’s quite different, nothing is loaded automaticcaly, if you look a Trails project you can see in each directory an index.js, if you take a look at it you can see it only loads each file. Like this if you want to exclude some files from Trails you can, it will only load the files put in the index.js. By default if you use the yo generator to generate your files it will add it automatically under the index.js, if you don’t, don’t forget to add it on it or it will be ignored. Mistake can append because of this. With this kind of loading system that’s mean if you don’t want an MVC architure, you can change it to whatever you want, but files must be in the index.js at the end.

2) Web server

Sails

As we speak, Sails use express 3 as web server, I don’t have to remind you that this version of express is more de deprecated, it’s even not maintened anymore by the express team. They work to migrate to express 5 (no 4…) but it’s look like a big work to do this. You can’t choose another web server than express.

Trails

In Trails you can choose your web server, for now there Hapi or Express4 but more will come like Koa or even a Sails server (if you want to migrate it can be a first step). When you create a new Trails project, you will choose whitch web server you want to use. This will affect the way you write your policies and controllers, if you choose Hapi, you have to write them with the Hapi API, if Express4, then use Express4 API…

3) ORM

Sails

Sails come with Waterline at first, you can change to another ORM, but officially there only Waterline and you have to disable it to be able to use another one.

Trails

Trails support multiple ORM, the first one is Waterline too but it’s not buddle with Trails, if you don’t want it just don’t install it and install the ORM you want to use (Bookshelf for example).

4) Web Socket

Sails

Socket.io is the framework used by Sails, it’s unbedded dirrectly in it. You can use another but have to disable socket.io first.

Trails

Once again Trails doesn’t have an one official web socket framework. You can choose witch one you want or create your own and don’t install the Trails one.

5) Assets manager

Sails

Grunt is used by default to manage assets but you are free to use Gulp or anything.

Trails

Trails doesn’t have an asset manager by default, you have to install Webpack, Gulp or the one you want.

6) Automatic REST API

Sails

Sails call this Blueprints, you can configure them to enable/disable what you don’t want to be generate automatically.

Trails

Trails call this Footprints, you have to install the module to be able to use and configure them.

7) Module/Plugin system

Sails

Sails call his modules hooks they can be under your project or install with npm, with some work you can embed some MVC API in it that will be load/merged in Sails.

Trails

Trails have two systems, the first one is trailpacks, there are loaded from your main.js config file and can embed MVC API and configuration files that will be automaticaly merge with Trails project. They can’t be under your project, they have to be installed with npm.
The second system is microservices, I will not talk about it for now because it’s not define yet but take a look at it on github later.

8) JavaScript

Sails

Sails use basic JavaScript ES5, it can run on Node 0.11 and more.

Trails

Trails use ES2015 (ES6), that allow developpers to use classes, const/let declarations and more… (see http://es6-features.org/#Constants), it can run on Node 4.0.0 and more.

Conclusion

Now you know all about the differences between this two MVC Node.js frameworks. You can make your choice and start coding ! for now Sails is in v0.12 and Trails is in beta (7).

]]>http://blog.jaumard.com/en/2016/04/01/sails-vs-trails/feed/7Create a Trailpack for Trailshttp://blog.jaumard.com/en/2016/01/06/create-a-trailpack-for-trails/
http://blog.jaumard.com/en/2016/01/06/create-a-trailpack-for-trails/#respondWed, 06 Jan 2016 22:45:51 +0000http://blog.jaumard.com/?p=58Hi guys ! Here you will learn how to create a customized Trailpack for Trails. You will see project’s structure and how to run and test it before publishing on npm. Hope you’re ready !

NOTE: This tutorial assumes that you have previously installed Node.js 4.0.0 (or more) on your machine, also have a Trails project created (see Getting started with trails).

What is a Trailpack ?

If you followed my first tutorial carefully, you already know this ! But I do not blame you if you forgot ^^, a Trailpack is a simple module which extends Trails functionalities. It can be a web server like express4, a tasks runner like grunt…

Installation

Trailpack can be generated with yeoman. So if you didn’t install it, you should do it now :

WARNING: if you don’t use yo to generate API, don’t forget to add those into index.js as in following: if it’s a model, add it in models/index.js, if it’s a controller add it in controllers/index.js… If your file is not in index.js, it will be ignored !

WARNING 2:
If your Trailpacks need Models, Controllers and Policies so your class must implement trails-model, trails-controller and trails-policy in order to work with more than one orm/webserver. Those class need to implement Waterline interface definition (models) and Hapi (controllers, policies).

Configuration

You can put your own file’s configuration here. As you can see, a trailpack.js file communicates Trails you provide some APIs and configuration to the project (provides field). But it allows you too to configure the lifcycle of your trailpack. Indeed, if your trailpack needs to wait for some events or emit ones, you can put those here :

First, you want to rename Archetype by your Trailspack’s name (here MyTrailpack). As you can see, you have three methods in it :

validate

configure

initialize

WARNING : if you want to create a Web Server or ORM compatible Trailpack you need to use trailpack-webserver / trailpack-datastore instead of trailpack. For more information see those repo : trailpack-webserver and trailpack-datastore

Validate

Validate function is the first called. As it’s named, you can check if the configuration of your trailpack is correct (if it’s needed). For example, the hapi trailpack checks if web server configuration under config/web.js is correct. In this tutorial we’ll just add some log.

At last, we call Promise.resolve() to tell Trails our Trailpack is valid, if it’s not we can call Promise.reject()

Configure

Next come the configure function, this method is called after all events waiting in your Trailpack (see Configuration section below).
You can configure your Trailpack removing the hapi Trailpack. Here, it’s created the web server and configured all routes bind controllers and policies…

Start your server with npm start and you should see if your trailpack is executed :

Now you are ready to create some new cool Trailpacks ! But don’t waste your time fooly and check first, on npm, if the Trailpack you want does not already exist

See you guys later !

]]>http://blog.jaumard.com/en/2016/01/06/create-a-trailpack-for-trails/feed/0Getting started with Trailshttp://blog.jaumard.com/en/2016/01/05/getting-started-with-trails/
http://blog.jaumard.com/en/2016/01/05/getting-started-with-trails/#commentsTue, 05 Jan 2016 18:36:12 +0000http://blog.jaumard.com/?p=7Hi everyone ! This is my first tutorial, so be indulgent please :).
You will learn some basic information about Trails, how to create a Trails project, and the basic structure of a Trails project. Don’t hesitate to comment if I miss something or if I’m not clear enough.

NOTE: This tutorial assumes that you have previously installed Node.js 4.0.0 (or higher) on your machine. Some knowledge of MVC and Javascript is needed.

What is Trails ?

Trails is a new Node.js MVC framework write in ES2015 (ES6). It’s a modular framework which allows you to choose your every day framework(s) and make them work together.

Installation

Trails use yeoman to generate a basic project. So if you haven’t installed it yet, do it now :

npm install -g yo

Now install the yeoman generator for Trails :

npm install -g generator-trails

You are now ready to create your first application !

Create a project

Creating a project is very easy. Just follow this guide :

mkdir tutoTrails;
cd tutoTrails;
yo trails;

You should see something like this :

The first step is to choose the web server. By default it’s hapi but you can choose another one if prefer. This choice will affect how you write your policies and controllers, but we will se this later.

Waterline will take care of database/table creation for you, by default.

You can create a new model with this command :yo trails:model myModelName

WARNING: if you don’t use yo to generate your models don’t forget to add them under api/models/index.js or it will be ignored.

Controllers

If you remember, when setting up our Trails app, we chose hapi as our web server, so all controller functions have to look like hapi middleware functions.
Here is what you will have in ViewController.js :

Here is what you should see when you open your browser on http://localhost:3000. But how is this function mapped to route / ? You will see this in Config section.

If your controller(s) extends ‘trails-controller’ then your controller(s) methods must have to implement only hapi interface (with request and reply) and your controllers will work with hapi, express4 or any webserver trailpack who support standard Trails controllers.
If your controllers doesn’t extends this class like the example above, your controller methods can implement native interface of the choosen webserver (request, reply for hapi, req, res, next for express4…).

You can create new controllers with this command :yo trails:controller myControllerName

WARNING: if you don’t use yo to generate your controllers don’t forget to add them under api/controllers/index.js or it will be ignored.

Services

Services are basically the brains of your project, they look like (DefaultService.js) :

NOTE: trailpacks*: All modules used by Trails are call Trailpack. For example to use Hapi as web server, Trails uses a Trailpack named trailpack-hapi. Don’t hesitate to look on npm to find some cool Trailpacks.

WARNING: if you add some config files don’t forget to add them under config/index.js or it will be ignored.

You now have the basic knowledge to make a simple Trails project!

More ?

Did you notice that when you start your project you have trails>?

Try to write app, app.api.controllers, app.api.services or even get('/api/v1/default/info'). Cool isn’t it ?