Creating a new Sails project

What's in a Sails project

Summary

Now that we've created our first Sails project, let's explore its folder structure and configuration files.

1. api/ - Main Application Logic

You'll spend most of your time in this folder. It contains:

  • controllers/ – Handle incoming requests.

  • helpers/ – Reusable functions available throughout the app.

  • models/ – Define database tables (or collections in MongoDB).

  • policies/ – Access control mechanisms to guard routes or controllers.

2. assets/ - Frontend Assets

Contains static files like:

  • dependencies/ – External frontend libraries (e.g., Bootstrap).

  • images/ – Image assets.

  • js/ – Frontend JavaScript.

  • styles/ – CSS stylesheets.

  • templates/ – Frontend templates (if using Vue, etc.).

  • favicon.ico – Website icon.

3. config/ - Application Configuration

Handles all app settings:

  • env/ – Configuration for different environments (e.g., production, staging).

  • blueprints.js – Controls auto-generated routes for models.

  • bootstrap.js – Runs custom startup code when the app starts.

  • custom.js – Holds app-wide custom settings.

  • datastore.js – Defines database connections.

  • globals.js – Configures globally available variables.

  • http.js – Controls middleware and HTTP settings.

  • i18n.js – Handles internationalization.

  • local.js – Machine-specific settings (ignored by Git).

  • log.js – Configures logging behavior.

  • models.js – Global settings for all models.

  • policies.js – Maps policies to controllers.

  • routes.js – Defines custom routes.

  • security.js – Security settings (CORS, CSRF, etc.).

  • session.js – Session configuration.

  • sockets.js – WebSocket settings.

  • views.js – Configures the view engine (e.g., EJS).

4. tasks/ - Grunt Tasks

Manages frontend asset compilation via Grunt.

5. views/ - Frontend Templates

  • layout.ejs – Main layout template for all pages.

  • pages/ – Stores individual page templates.

  • 404.ejs / 500.ejs – Default error pages.

6. Root-Level Files

  • .editorconfig – Enforces consistent code style.

  • .eslintignore / .eslintrc – Configures ESLint for code linting.

  • .gitignore – Specifies ignored files in Git.

  • .npmrc – NPM settings.

  • .sailsrc – App-wide Sails configuration.

  • app.js – The entry point of the application.

  • Gruntfile.js – Manages Grunt tasks.

  • package.json – Lists project dependencies.

Transcript

Alright! Now that we've created our Sails project called blog, let's explore its structure and understand what each file does.

1. api/ - Where the Magic Happens!

This is where you'll spend about 80% of your time.

  • controllers/ – The entry point for handling requests.

  • helpers/ – Reusable functions accessible throughout your Sails app.

  • models/ – Defines your database schema (tables or collections).

  • policies/ – Access control for controllers and actions.

2. assets/ - Frontend Assets

This folder contains all static frontend assets.

  • dependencies/ – For third-party libraries (e.g., Bootstrap).

  • images/ – Stores images.

  • js/ – Frontend JavaScript files.

  • styles/ – CSS stylesheets.

  • templates/ – If you're using a frontend framework like Vue.

  • favicon.ico – The website’s icon.

3. config/ - Configuring the App

Sails allows a high level of customization through configuration files.

  • env/ – Configurations for different environments like production or staging.

  • blueprints.js – Controls auto-generated CRUD APIs.

  • bootstrap.js – Runs custom startup logic when the app starts.

  • custom.js – Stores custom app settings.

  • datastore.js – Defines database connections.

  • globals.js – Configures globally available variables.

  • http.js – Middleware and HTTP settings.

  • i18n.js – Handles internationalization (multi-language support).

  • local.js – Local-only settings (ignored by Git).

  • log.js – Controls application logging.

  • models.js – Global settings for models.

  • policies.js – Maps policies to controllers.

  • routes.js – Defines custom routes.

  • security.js – Security settings like CORS and CSRF protection.

  • session.js – Manages session-based authentication.

  • sockets.js – WebSocket configuration.

  • views.js – Configures the view engine (default: EJS).

4. tasks/ - Automating Frontend Tasks

This is for Grunt tasks, which handle frontend asset management.

5. views/ - Frontend Templates

  • layout.ejs – The main layout file.

  • pages/ – Stores page-specific templates.

  • 404.ejs / 500.ejs – Default error pages.

6. Root-Level Files

These files control project-wide settings:

  • .editorconfig – Ensures consistent coding styles.

  • .eslintignore / .eslintrc – ESLint configuration for enforcing code quality.

  • .gitignore – Specifies files to exclude from Git.

  • .npmrc – NPM settings.

  • .sailsrc – Sails-wide configuration settings.

  • app.js – The entry point of your Sails application.

  • Gruntfile.js – Defines frontend automation tasks.

  • package.json – Contains metadata and lists dependencies.

Final thoughts

Now you understand what happens when you run:

sails new blog

This creates a fully structured Sails app, with everything from API logic to frontend assets and configuration files.

Full Course

$
29.99

USD

plus local taxes
Buy Now