Thank you for purchasing the MEAP for Sails.js in Action . To get the most benefit from this book, youll want to know how to build a web page, and be familiar with the DOM, HTML, and CSS. We assume a fair bit of knowledge with JavaScript, including objects and arrays, functions, and scope, as well as events and callbacks on the front-end. Finally we assume that you have experience making AJAX requests to a server.
The pace at which new technologies have emerged to deliver applications over the web has been nothing short of astounding. Were in the middle of a technology renaissance in which two important innovations have emerged.
The first is the transition away from a purely server-based approach to building user interfaces to a more client-focused Single Page App (SPA) approach.
The second is the widespread adoption of JavaScript outside the browser and, more specifically, on the server via Node.js.
These two innovations have forced a reassessment of how developers build web applications. And it is these two innovations which drove Sails early design.
Sails makes it easy to build truly custom, enterprise-grade Node.js applications. The key to a successful project, however, is a complete understanding of the concepts youll use to build an application, coupled with a sustainable and repeatable development process.
Were going to strive to have some fun in this book while teaching you these concepts, techniques, and tools of the Sails Framework. Each chapter will build on its predecessor, with reinforcing, real-world examples. By the end of the book, youll be prepared to deliver web applications that exceed expectations within a process that you can easily repeat.
Please be sure to post any comments, suggestions, or questions in the books forum. Your feedback will help us develop the best book possible.
1 Getting Started
This chapter covers
Modern web development
Understanding the architecture of the Sails framework
Where Sails fits into modern web development
Installing the necessary components of the technical stack
Setting up the tools of your development environment
Too often, back-end programming is put on a pedestal, where only highly trained and disciplined experts are worthy. Thats baloney. Back-end programming isnt rocket sciencebut that doesnt mean its easy. It means that for those new to it, you just need a healthy curiosity and a powerful framework like Sails to get started. If you already have experience with back-end programming in a language other than JavaScript, the transition can also be frustrating. Shifting from synchronous to asynchronous patterns can take some time to master. Whether new or experienced, Sails will make this transition much easier. Our goal is to provide an entertaining, practical, gap-free path to understanding Sails as well as modern back-end web development. For a brief history of Sails, see appendix A.
1.1 What is Sails?
Sails is a JavaScript back-end framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture. It's especially good for building chat, real time dashboards, or multiplayer games; but you can use it for any web application project - top to bottom.
The book is targeted at two types of developers. First, is a developer who has front-end experience and is looking to become a full-stack programmer using JavaScript, a language they already know. Second, is a developer that has back-end experience in a language other than JavaScript and is looking to expand their knowledge to Node.js. In either case, familiarity with HTML, CSS, and JavaScript is expected, as well as experience with making AJAX requests. Most important is a healthy curiosity of how to build a web application.
1.2 What can you build with Sails?
Whether youre a front-end developer seeking to expand your back-end knowledge, or a server-side developer unfamiliar with using Node and JavaScript on the back-end, the common denominator we all share is a desire to create web applications. Sails is designed to be compatible with whatever strategy you have for building your front-end, whether it be Angular, Backbone, iOS/ObjC, Android/Java, or even a headless app that just offers up a raw API to be used by another web service or your developer community. Sails is great for building everyday back-end apps that handle HTTP requests and WebSockets. It isnt a good approach for building the client-side of your application-- that part is completely up to you. If you end up changing your approach (e.g. switching from Backbone to Angular) or building a new front-end entirely (e.g. building a Windows Phone native app), your Sails app will still work.
Be warned youre about to experience a buzzword bonanza. If you see a term you dont recognize, dont worrywell return to these concepts in detail later in the book.
So what type of applications can you build? Sails excels at building:
Hybrid Web Applications - applications that combine a JSON API with server-rendered views, that is, in addition to an API, this type of application can serve dynamic (i.e. personalized) HTML pages, making it suitable for use cases which demand SEO (search engine optimization). These applications often use a client-side JavaScript framework (e.g. Angular, Ember, React, etc.), but they dont necessarily have to. Examples of hybrid web applications you might be familiar with are Twitter, GitHub, and Basecamp.
Pure APIs applications that fulfill requests from one or more independent front-end user interfaces. We say independent because the front-end doesnt have to be delivered by the same server that is providing the JSON API, or even by a server at all. This umbrella category includes SPAs (single-page apps), native mobile applications (e.g. iOS and Android), native desktop applications (e.g. OSX, Windows, Linux), and the venerated IoT (Internet of Things). Many mobile-first products (think Uber, Instagram, Snapchat) start off as pure APIs.
If you arent sure which category your application falls into, dont worry: the concepts overlap. A pure API is to a hybrid web application as a square is to a rectangle. Well spend the first half of this book building a pure API, and the remaining chapters extending and maintaining it as it transitions into a hybrid web application.
1.3 So why Sails?
100% JavaScript - Like other MVC frameworks, Sails is built with an emphasis on developer happiness and a convention-over-configuration philosophy. But Node.js takes this principle to the next level. Building on top of Sails means your app is written entirely in JavaScript, the language you and your team are already using in the browser. Since you spend less time context-shifting, you're able to write code in a more consistent style, which makes development more productive and fun.
The authors of this book can both attest to how nice it is to work with one language instead of constantly switching back and forth between JavaScript and whatever back-end language your company or customers are using. The best part? It means you get really good at it.