🎓 A1Learner
🔍
0 of 50 lessons completed

Preparing for Production

Preparing for production means making the app ready for real use. The app must run without the dev tools and survive a restart.

Scripts in package.json

The start script runs the API without dev tools.

"scripts": {
  "start": "node server.js",
  "build": "vite build"
}

Production mode

NODE_ENV tells the app which mode it runs in.

if (process.env.NODE_ENV === 'production') {
  console.log('Running in production');
}

Example

process.env.NODE_ENV = 'production';
process.env.MONGO_URI = 'mongodb://localhost:27017/notes';
process.env.PORT = '3001';
process.env.JWT_SECRET = 'a-long-dev-secret-value';

const checks = [
  ['NODE_ENV is production', process.env.NODE_ENV === 'production'],
  ['MONGO_URI is set', Boolean(process.env.MONGO_URI)],
  ['PORT is set', Boolean(process.env.PORT)],
  ['JWT secret is long enough', process.env.JWT_SECRET.length >= 12]
];

for (const [name, passed] of checks) {
  console.log((passed ? 'PASS' : 'FAIL') + ' ' + name);
}
console.log('Ready for production: ' + checks.every(([, passed]) => passed));

Key points

  • Production is the live app that real people use.
  • The API needs a start script that runs without nodemon.
  • The database address must come from the server settings, not from local files.
  • Error messages must stay short and hide private detail.

In this example, the script sets the production values first. It then runs four checks on those values. Each check prints PASS when the value is ready. The last line prints the full result.

Note

The dev servers are switched off before the app goes live.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What must the API start script do in production?