Shoulder.dev Logo Shoulder.dev

Deploying benhall/express-demo to a Production Environment

Scenario: You have developed an Express application using the benhall/express-demo project and are now ready to deploy it to a production environment. The goal is to configure the server, set up the database, and use a process manager for production deployment.

Solution:

  1. Configure the Express Application for Production:

First, let’s make some modifications to the app.js file to prepare it for production. Update the following lines:

// Set NODE_ENV to 'production'
process.env.NODE_ENV = 'production';

// Enable gzip compression
const compression = require('compression');
app.use(compression);

// Set up view engine to serve .hbs files
app.engine('hbs', exphbs.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
  1. Set Up the Database:

Assuming you’re using a PostgreSQL database, you’ll need to create a new database and user for your production environment. You can use a tool like pgAdmin or the createdb command to create the database.

Next, update the db.js file with the new database credentials:

const { Pool } = require('pg');

const pool = new Pool({
user: 'your_production_user',
host: 'your_production_host',
database: 'your_production_database',
password: 'your_production_password',
port: 5432,
});

module.exports = {
pool,
};
  1. Use a Process Manager for Production Deployment:

To ensure your application stays running in production, you can use a process manager like PM2 or systemd. In this example, we’ll use PM2.

First, install PM2 globally:

npm install -g pm2

Next, start your application using PM2:

pm2 start app.js --name "express-demo" --watch

This command will start your application and keep it running even if the terminal is closed. The --watch flag will automatically restart the application if any changes are detected.

  1. Set Up a Reverse Proxy:

If your production environment is behind a reverse proxy, you’ll need to update your app.js file to listen on the correct port and proxy requests to the correct address.

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

// Set up view engine to serve .hbs files
app.engine('hbs', exphbs.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));

// Proxy requests to the correct address
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'production', 'index.html'));
});

app.listen(port, () => {
console.log(`App listening on port ${port}.`);
});
  1. Set Up a Continuous Integration (CI) System:

To ensure that your application is always in a deployable state, you can set up a CI system like GitLab or Jenkins. This system will automatically build and test your application whenever changes are pushed to the repository.

  1. Set Up Monitoring:

To keep an eye on your application’s performance and health, you can use a monitoring tool like New Relic or Datadog. These tools can help you identify and resolve issues before they affect your users.

Tests:

  1. Verify that the application is running in production by visiting the production URL.
  2. Verify that the database is set up correctly by connecting to it using a tool like pgAdmin or the psql command.
  3. Verify that the application is serving the correct content by checking the response headers and the content of the pages.
  4. Verify that the application is handling requests correctly by testing the endpoints using a tool like Postman.
  5. Verify that the application is restarting correctly when changes are pushed using PM2 or the process manager of your choice.
  6. Verify that the CI system is building and testing the application correctly by checking the build and test logs.
  7. Verify that the monitoring tool is correctly reporting the application’s performance and health.