A Simple Sails.js Example App

This tutorial takes you through the steps of creating a simple Sails.js application. This is for you if:

  • You're just getting started with Node.js and Sails.js and want an example of how to create a simple Sails.js application.

  • You're already familiar with Sails.js development but are interested in a better dev-to-production workflow, and don't like doing DevOps.

I'll be using Nanobox as it's the quickest way to get started, and also ensures that I won't have to worry about creating, configuring, and managing my applications infrastructure when it's ready for production.

Note: If you've never used Nanobox before, you'll first need to create a free account and download Nanobox Desktop. Also, Don't worry about whether or not you have Node.js or Sails.js installed. You won't need them!

Create a Simple Sails Application

First, create the project folder and change into it:

mkdir nanobox-sails && cd nanobox-sails

Setup

Note: All nanobox commands must be run from the root of your project.

A simple config file, called a boxfile.yml describes your app's infrastructure. Nanobox uses it when creating any of your app's environments like development, staging, or production.

At the root of your project, create a boxfile.yml with the following:

run.config:
  engine: nodejs

Add a Local DNS Alias

To make your app easily accessible from the browser, you can add a local DNS alias with the nanobox dns command:

nanobox dns add local sails.dev

Generate a Sails.js App

Normally, you'd need to have Node.js and Sails.js already installed on your local machine, and would have used the Sails generator to create your app.

I'll show you how to do it without having them installed. One of the great things about Nanobox is you can easily dive into any new language or framework without having to get your local environment all setup before hand.

After you've added your boxfile.yml run:

nanobox run

This will read your boxfile.yml, create an isolated development environment, and drop you into a console within that environment.

The nodejs engine specified in your boxfile.yml installs Node.js and yarn/NPM automatically, so you'll just need to install Sails:

# install sails so you can use it to generate your application
yarn add sails

# cd into the /tmp dir to create an app
cd /tmp

# generate the sails app
sails new app

# cd back into the /app dir
cd -

# enable shell to interact with hidden files
shopt -s dotglob

# copy the generated app into the project
cp -a /tmp/app/* .

# exit the console
exit

Run Your App

You can run your app using one of two ways depending on where you are.

From inside a nanobox console:

sails lift

From outside a nanobox console:

nanobox run sails lift

Thats it! You're application should be running and you can visit it at sails.dev:1337 (or whatever alias you created above)

Posted in Node.js, JavaScript, Getting Started, Nanobox, Sails.js