Learn Sails.js - Part 3

von Manuel am 07.09.2025 um 12:00 Uhr

Aye, aye, Boatswain! Let’s get this ship looking respectable. It’s time to hoist the banners and polish the quarterdeck.


Part 3: Designing the Quarterdeck (Views & Assets)


The Captain’s Briefing (Background and Theory)

So far, our ship can respond to hails, but only by shouting back raw data (JSON). That’s fine for ship-to-ship communication, but it’s not very welcoming for distinguished visitors who expect to come aboard and see a polished deck. This module is about creating that polished deck: the View layer.

From Data to Decks: res.view()

In the last module, we used res.json() to send back data. To send back a full HTML page, we use a different command: res.view(). When you call res.view('some-page'), Sails looks inside your views/pages/ folder for a file named some-page.ejs and sends it to the user’s browser as a web page.

EJS: The Magic Parchment

The files inside your views/ folder are not plain HTML. They are .ejs files, which stands for Embedded JavaScript. Think of EJS as a magic parchment: it looks and feels like normal HTML, but you can embed special tags into it to print dynamic data.

The Master Blueprint: Layouts

Building every single page from scratch would be tedious. You’d have to copy and paste your navigation bar, header, and footer on every page. Sails solves this with a layout file.

Look in views/layouts/layout.ejs. This is your master ship blueprint. It contains the common HTML structure for your entire site (<html>, <head>, <body>, etc.). Notice the special tag somewhere in the middle: <%- body %>.

When you call res.view('pages/homepage'), Sails first renders homepage.ejs, then takes the resulting HTML and injects it into the layout.ejs file right where <%- body %> is. This way, you only need to define your main structure once!

Paint and Flags: The Asset Pipeline

How do you add styling (CSS), front-end interactivity (JavaScript), or images? You place these files into the assets/ directory.

This isn’t just a folder for storage. Sails has a powerful Asset Pipeline. When you run sails lift, Sails automatically scans these folders. It will compile, combine, and minify these files for you (in production), and most importantly, it will automatically inject the necessary <link> and <script> tags into your layout.ejs file. You don’t need to manually link to your stylesheets; just drop them in the right folder, and Sails handles the rest.


Key Concepts Checklist


Mission Log: Quest - “The Captain’s Log”

Our ship needs a proper logbook. We will create a new page at /logbook, display the Captain’s name dynamically, and give the page some custom styling.


Mission Debrief (Review & Outcomes)

Splendid work, Boatswain. The quarterdeck is immaculate. You have successfully separated logic from presentation.

This is the MVC pattern in its full glory. Your application is no longer just a data machine; it’s a presentable web application. You can now create as many pages as you wish, each with its own-logic and styling.


Rewards