Express.js- Production best practices | Best practices of Express.js 2024

Express.js- Production best practices | Best practices of Express.js 2024

Express.js 2024- Production best practices of express.js | Express.js developer guide 2024

Overview

This article discusses performance and reliability best practices for Express applications deployed to production.

Use of gzip compression

Gzip compressing can greatly decrease the size of the response body and hence increase the speed of a web app. Use the compression middleware . Install Gzip by running npm i compression

import compression from 'compression'
import express from 'express'
const app = express()
app.use(compression())

For a high-traffic website in production, the best way to put compression in place is to implement it at a reverse proxy level like Nginx

Do logging correctly

For debugging and for logging app activity (essentially, everything else). Using console.log() or console.error() to print log messages to the terminal is common practice in development. But these functions are synchronous. If you’re logging for purposes of debugging, then instead of using console.log(), use a special debugging module like debug.

import debug from 'debug'
app.use('/api/greeting', (request, response) => {
  const name = request.query ? request.query.name : undefined;
  //log name in debugging
  debug('name: '+name);
  response.send({content: `Hello, ${name || 'World'}`});
});

Don’t use synchronous functions

You can read again, above we have seen few example of synchronous function like console.log() I have use a special debugging module like debug. But let's learn these two topic.

What is Synchronous in JavaScript?

As its base JavaScript language is synchronous. Synchronous means the code runs in a particular sequence of instructions given in the program. Each instruction waits for the previous instruction to complete its execution.

console.log('First');
console.log('Second');
console.log('Third');

// output
First
Second
Third

What is Asynchronous in JavaScript?

Asynchronous code execution allows to execution next instructions immediately and doesn't block the flow because of previous instructions.

console.log('First');

// Set timeout for 2 seconds
setTimeout(() => 
console.log('Second')
, 2000);

console.log('Third');

//output
First
Third
Second

Handle exceptions (try/catch) properly

Error handling is a pain, and it’s easy to get by for a long time in Node.js without dealing with errors correctly. However, building robust Node.js applications requires dealing with errors properly.

How to handle MongoDB error while connecting?

Look at the following example that how we handle mongoDB database connection. I used try/catch

import mongoose from 'mongoose';
export const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGO_DB as string, { 
    }).then(() => console.log("MongoDB Connected")).catch(() => console.log("MongoDB is not Connected"));
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

Did you find this article valuable?

Support Shivam Sharma by becoming a sponsor. Any amount is appreciated!