Sails.js & Vue.js - Part 1

von Manuel am 02.11.2025 um 12:00 Uhr

Lord High Admiral. We will begin the refit. Before we outfit the fleet with the latest enchantments, a true commander must understand the traditional craft. This first step will ground us in the fundamentals of how a modern front-end communicates with our Sails vessel using the ship’s existing systems.


Part 1: The Traditional Refit (Vue & The Asset Pipeline)


Chief Architect’s Briefing (Background and Theory)

Our ships are currently built with EJS templates rendered on the server. When you visit /logbook, Sails compiles the HTML and sends a complete page to your browser. Our goal is to enhance these pages, not replace them entirely… yet.

We will treat Vue.js as a specialist crew member that we bring aboard to manage one specific, complex part of the deck. To do this, we will leverage Sails’ built-in Asset Pipeline.

The assets/ directory in your Sails project is a special place. Sails automatically processes the files within it. Any JavaScript file you place in assets/js/ will be automatically included as a <script> tag in your master layout file (views/layouts/layout.ejs). This is the system Sails has always used for front-end JavaScript.

Our strategy is as follows:

  1. We will add the Vue.js library to our project via a CDN link in our layout file.
  2. We will create a placeholder <div> in one of our EJS views. This will be the “station” where our Vue crew member works.
  3. We will write our Vue application in a separate JavaScript file within assets/js/.
  4. When the page loads, this script will find the placeholder <div> and “mount” our Vue application onto it, bringing that section of the page to life.
  5. Once alive, our Vue component will make a standard network request (fetch) to a Sails API endpoint to get its data, just like any third-party client would.

This approach creates a “reactive island” on an otherwise static page. It is powerful for enhancing specific components but, as we will see, it reveals why a more integrated solution like Inertia.js is so desirable for full applications.


Key Concepts Checklist


Mission Log: Quest - “The Live Cargo Manifest”

We will create a new, simple application. The goal is to display a ship’s cargo manifest. The manifest list itself will be rendered and managed by Vue, fetching its data from a Sails API endpoint.


Mission Debrief (Review & Outcomes)

Excellent work, Admiral. You have successfully refitted a traditional vessel with a modern, reactive component. You now understand the fundamental mechanics: a server-rendered page loads, a JavaScript library is included, it mounts to a DOM element, and it fetches data from an API to manage its own small piece of the world.

However, consider the complexities. We had to manually create API endpoints for our front-end. We have Vue syntax sitting inside an EJS file. We wrote a manual fetch call. If we wanted to add a form to add new cargo, we’d have to write more manual fetch calls with POST requests and handle the response.

This approach works, but it feels… disconnected. It’s like our specialist Vue crew has to shout across the deck to the Sails crew to get anything done. What if they could communicate telepathically?

This is the very problem that Inertia.js was born to solve.


Rewards