Specify a different Sails port

Summary

In this Sailscasts screencast, we explore the different ways to change the port that a Sails application runs on. By default, Sails serves applications on port 1337, but this is merely a fallback port. There are multiple ways to specify a custom port, each with its advantages and use cases:

  1. Configuring config/local.js

    • This file is for local development settings.

    • Setting the port here allows each developer to configure their preferred port individually.

  2. Using config/misc.js

    • A global top-level setting file for defining ports used across the entire team.

    • If the file doesn’t exist, it can be created.

  3. Passing the port as an environment variable

    • The port can be set dynamically using the sails lift --port <port_number> command.

  4. Defining the port in config/env/*.js

    • Useful for setting environment-specific ports, such as for production or staging.

    • The development.js file can also be used but is often unnecessary when config/local.js is already in place.

Sails will use port 1337 only if no port is specified in any of the above methods. The recommended approach for setting a project-wide port is using config/misc.js, whereas config/local.js is best suited for individual developers.

Transcript

Hey there, my friend! Welcome to another Sailscasts screencast. In this screencast, we're going to explore how to change the port that Sails runs on.

By default, when you run sails l or sails lift, Sails serves your application on port 1337. However, this isn't the default port Sails expects—it's simply a fallback when no other port is specified.

Setting the Port

There are multiple ways to configure the port in Sails, each with its advantages and use cases. Let's go through them.

1. Configuring config/local.js

First, let's open the application in VS Code. This is the blog application from the Getting Started with Sails course. We'll open config/local.js, which is used for local development settings.

If we add:

port: 3000

and then restart Sails with sails lift, the application now runs on port 3000. This method is great for allowing each developer to use their preferred port without affecting the team.

2. Using config/misc.js

Another way to set the port is by creating or modifying config/misc.js. This file contains global settings for the entire project.

If it doesn’t exist, create it and add:

module.exports = {
  port: 8080
};

After restarting Sails, it now runs on port 8080. This method is best for setting a default port for the entire team.

3. Passing the Port as an environment variable

You can also set the port dynamically when running the sails lift command. Run:

sails lift --port 9000

This starts the application on port 9000 without modifying any configuration files. This method is useful for temporary port changes.

4. Using config/env/*.js

Sails supports environment-specific configurations. Inside config/env/, you’ll find different files such as production.js. You can also create a development.js file and set:

module.exports = {
  port: 8000
};

Restarting Sails now runs it on port 8000. While this method is available, I personally prefer using config/local.js for local development settings.

Conclusion

In summary, you can specify the Sails port in different ways:

  • For personal use: config/local.js

  • For team-wide consistency: config/misc.js

  • For one-time changes: Passing --port in the command line

  • For environment-specific settings: config/env/*.js

If no port is found in these settings, Sails will fall back to 1337.

That’s it for this screencast! I hope this helps you understand how to change the port for your Sails application. Happy coding!

Lastest courses

course.title

Build 50 Products in 50 Days

Focus on shipping full-stack JavaScript web apps instead of chasing trends. A course bundled with 50 ready-to-ship products.

course.title

Getting started with Waterline

Master the fundamentals of the built-in Sails ORM - Waterline

course.title

Getting started with Sails

Learning everything you need to know to get up and running with Sails

Browse All Courses