All preparations are complete. The crew is drilled, the ship is secure, and the holds are managed. But we are still anchored in a safe, familiar harbor. The time has come to chart a course for the open sea, the unpredictable, high-performance world of production.
Part 9: Charting the Open Seas (Configuration & Deployment)
- Current Rank: First Mate
- Objective: To prepare your Sails application for a live production environment by managing environment-specific configurations and learning the process of deploying it to a server.
The Captain’s Briefing (Background and Theory)
So far, we have operated in a private shipyard (localhost). Here, the waters are calm. Mistakes are cheap, our tools are laid out for us (sails lift auto-restarts the server), and performance isn’t a primary concern.
The open sea, a production environment, is different. It’s treacherous and demanding. Visitors expect a ship that is fast, efficient, and always available. We can’t have our First Mate on the deck constantly restarting the engine by hand. The ship must be configured to be lean, resilient, and ready for battle.
Environment-Specific Blueprints
Sails has a brilliant system for this: the config/env/ directory. Sails knows which environment it’s in based on a system-level flag called NODE_ENV.
- When you run sails lift, by defaultNODE_ENVisdevelopment. Sails will look for a file atconfig/env/development.jsand merge its settings.
- When you run your app for production, you set NODE_ENVtoproduction. Sails then looks forconfig/env/production.jsand uses its settings instead.
This allows you to have completely different configurations for your shipyard and your sea voyages. Common differences include:
- Datastores: Using the temporary sails-diskin development, but a powerful, persistent database like PostgreSQL in production.
- Logging: Verbose, detailed logs in development to help you debug, but quieter, more essential logs in production to save resources.
- Security: Using different API keys, secrets, and credentials. You must never commit production secrets to your code repository. They should be set as environment variables on the server itself, which production.jscan then read.
- Performance: Sails automatically does amazing things in production mode, like minifying your CSS and JavaScript files and bundling them together into single files. This makes your ship “lighter” and drastically reduces page load times for your visitors.
Launching the Fleet: The Dockmaster (PM2)
You don’t just run sails lift on a production server and walk away. What happens if the app crashes? Who restarts it? This is the job of a Process Manager.
A process manager is a dockmaster for your live application. The most popular one for Node.js apps is PM2. It’s a simple tool that you run your app with. It will:
- Keep your app running in the background.
- Automatically restart the app if it ever crashes.
- Help you run your app on multiple CPU cores for better performance.
- Manage and rotate log files.
The command to launch your ship for its maiden voyage will be sails lift --prod. The --prod flag is the crucial signal that tells Sails to enable all its production optimizations and use your production.js configuration file.
Key Concepts Checklist
- Environment Configuration: Using different settings for development, testing, and production.
- NODE_ENV: The environment variable that tells Sails which mode to run in.
- config/env/production.js: The file containing all settings overrides for a live environment.
- Asset Minification/Bundling: Sails’ automatic process of optimizing CSS/JS for production.
- Process Manager: A tool (like- PM2) that keeps your live application running reliably.
- sails lift --prod: The command to start Sails in production mode.
- Secrets Management: The practice of keeping sensitive data like API keys out of your code.
Mission Log: Quest - “The Maiden Voyage Simulation”
We will prepare our ship for launch, configure its production-ready blueprints, and use a professional process manager to launch it, all safely on our local machine.
- Task 1: Prepare the Production Blueprints - In your config/directory, create a new folder namedenv.
- Inside config/env/, create a new file namedproduction.js.
- Add the following configuration. This tells Sails how to behave on the open sea. - // config/env/production.js module.exports = { datastores: { /*************************************************************************** * * * In production, you'll want to use a real database like PostgreSQL or * * MySQL. This is where you'd configure the connection settings. * * * ***************************************************************************/ // default: { // adapter: 'sails-postgresql', // url: 'YOUR_PRODUCTION_DATABASE_URL', // ssl: true, // }, }, log: { // In production, we only want to log 'info' level messages and above level: 'info' }, session: { // A secure, long, random secret for signing session cookies. // Replace this with your own secret! secret: 'a4f434a2a163aed4b72449856f6b158b' } };
 
- In your 
- Task 2: Install the Dockmaster (PM2) - PM2 is a command-line tool that should be installed globally. In your terminal, run: - npm install pm2 -g
 
- Task 3: Simulate the Maiden Voyage - We will now launch our ship in production mode. Notice the difference in the log output compared to sails lift.
- In your terminal, run: - sails lift --prod- You’ll see much less chatter, just the essential “ship is online” message. The log level from - production.jsis working! Press- CTRL+Cto stop it.
 
- We will now launch our ship in production mode. Notice the difference in the log output compared to 
- Task 4: Launch with the Dockmaster - This is the professional way to launch. We tell PM2 to start our app.
- Run this command from your project root: - pm2 start app.js --name "the-sea-serpent" -- --prod- pm2 start app.js: The basic command to start the app.
- --name "the-sea-serpent": Gives our running process a memorable name.
- -- --prod: This is critical. The double-dash (- --) tells PM2 to pass the- --prodflag directly to our- app.jsscript, putting Sails in production mode.
 
 
- Task 5: Manage Your Fleet - Your app is now running in the background! How do we check on it?
- List running processes: - pm2 list- You will see “the-sea-serpent” in the list with a status of “online”. 
- Check the ship’s logs: - pm2 logs the-sea-serpent
- Stop the ship: - pm2 stop the-sea-serpent
 
Mission Debrief (Review & Outcomes)
Land ho! The maiden voyage was a roaring success. You have successfully taken your application out of the development shipyard and prepared it for the rigors of the open ocean.
You now understand the critical difference between development and production environments and how to manage configurations for both. You have wielded a professional process manager, PM2, to launch, monitor, and control your application like a true fleet commander. While we only simulated this locally, these are the exact skills and commands you would use when deploying to a real server on a host like DigitalOcean or AWS.
Rewards & Promotion
- +350 Doubloons
- Achievement Unlocked: You have successfully configured and launched your application for a production environment. Your ship is ready to sail the world.
- Badge Earned: Seasoned Mariner🌊
 
- Badge Earned: 
- Promotion to: Captain
Take the helm, Captain. You have proven your mastery of this vessel, from its initial construction to its final launch. There is only one challenge remaining: to command a fleet of your own.