@nestjs/serve-static vs express-static-gzip
Static File Serving with Gzip Support Comparison
3 Years
@nestjs/serve-staticexpress-static-gzipSimilar Packages:
What's Static File Serving with Gzip Support?

Static file serving is a common requirement in web applications, where the server delivers files such as HTML, CSS, JavaScript, images, and other assets directly to the client's browser. This process is typically handled by middleware in web frameworks. Gzip (GNU zip) is a file compression algorithm that reduces the size of files, leading to faster transmission over the internet. When static files are served with Gzip compression, they are compressed on the server before being sent to the client, resulting in quicker load times and reduced bandwidth usage. Both @nestjs/serve-static and express-static-gzip are middleware solutions for serving static files, but they cater to different needs. @nestjs/serve-static is designed for NestJS applications, providing a straightforward way to serve static assets. In contrast, express-static-gzip is an Express middleware that specifically focuses on serving static files with Gzip compression, offering features like automatic compression, support for pre-compressed files, and customizable caching headers.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@nestjs/serve-static417,597
49964.6 kB107 months agoMIT
express-static-gzip144,611
15024.5 kB34 months agoMIT
Feature Comparison: @nestjs/serve-static vs express-static-gzip

Framework Integration

  • @nestjs/serve-static:

    @nestjs/serve-static is specifically designed for NestJS applications, making it a natural choice for projects built on this framework. It leverages NestJS's module system and dependency injection, ensuring a smooth integration.

  • express-static-gzip:

    express-static-gzip is built for Express applications, providing a specialized solution for serving static files with Gzip compression. It can be easily integrated into any Express app, but it does not offer the framework-specific features that @nestjs/serve-static provides.

Gzip Compression

  • @nestjs/serve-static:

    @nestjs/serve-static does not provide Gzip compression by default. However, you can enable Gzip compression in your NestJS application by using additional middleware, such as compression, alongside @nestjs/serve-static.

  • express-static-gzip:

    express-static-gzip automatically handles Gzip compression for you. It serves files with Gzip compression if they are available, and it can also compress files on the fly if they are not pre-compressed. This feature makes it a more efficient choice for applications focused on optimizing bandwidth usage.

Pre-compressed File Support

  • @nestjs/serve-static:

    @nestjs/serve-static does not support serving pre-compressed files out of the box. You would need to implement this functionality manually or use additional middleware to handle pre-compressed files.

  • express-static-gzip:

    express-static-gzip supports serving pre-compressed files (e.g., .gz files) natively. If a pre-compressed file is available, it will be served instead of the original file, which can significantly reduce load times for static assets.

Caching Control

  • @nestjs/serve-static:

    @nestjs/serve-static allows you to set caching headers for static files, but you need to configure them manually. This gives you flexibility in how you manage caching, but it requires additional setup.

  • express-static-gzip:

    express-static-gzip provides built-in support for caching headers, and you can customize them easily. It also allows you to set cache control headers for both compressed and uncompressed files, making it more feature-rich in this regard.

Code Example

  • @nestjs/serve-static:

    Serving Static Files in NestJS

    import { Module } from '@nestjs/common';
    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    import { ServeStaticModule } from '@nestjs/serve-static';
    import { join } from 'path';
    
    @Module({
      imports: [
        ServeStaticModule.forRoot([
          {
            rootPath: join(__dirname, '..', 'public'),
            serveRoot: '/static', // Optional: Custom route prefix
          },
        ]),
      ],
    })
    class AppModule {}
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      await app.listen(3000);
    }
    bootstrap();
    
  • express-static-gzip:

    Serving Static Files with Gzip in Express

    const express = require('express');
    const path = require('path');
    const expressStaticGzip = require('express-static-gzip');
    
    const app = express();
    
    app.use('/static', expressStaticGzip(path.join(__dirname, 'public'), {
      enableBrotli: true, // Enable Brotli compression
      orderPreference: ['br', 'gzip'], // Prefer Brotli over Gzip
    }));
    
    app.listen(3000, () => {
      console.log('Server is running on http://localhost:3000');
    });
    
How to Choose: @nestjs/serve-static vs express-static-gzip
  • @nestjs/serve-static:

    Choose @nestjs/serve-static if you are building a NestJS application and need a simple way to serve static files. It integrates seamlessly with the NestJS framework and is easy to configure.

  • express-static-gzip:

    Choose express-static-gzip if you are using Express and want to serve static files with Gzip compression out of the box. It is ideal for applications where reducing bandwidth and improving load times for static assets is a priority.

README for @nestjs/serve-static

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

@nestjs/serve-static package for Nest, useful to serve static content like Single Page Applications (SPA). However, if you are building MVC application or want to serve assets files (images, docs), use the useStaticAssets() method (read more here) instead.

Installation

$ npm i --save @nestjs/serve-static

Example

See full example here.

Usage

Simply import ServeStaticModule in your Nest application.

import { Module } from '@nestjs/common';
import { join } from 'path';
import { ServeStaticModule } from '@nestjs/serve-static';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client')
    })
  ]
})
export class ApplicationModule {}

API Spec

The forRoot() method takes an options object with a few useful properties.

| Property | Type | Description | | -------------------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | rootPath | string | Static files root directory. Default: "client" | | serveRoot | string | Root path under which static app will be served. Default: "" | | renderPath | string / RegExp | Path to render static app (concatenated with the serveRoot value). Default: * (wildcard - all paths). Note: RegExp is not supported by the @nestjs/platform-fastify. | | exclude | string[] | Paths to exclude when serving the static app. WARNING! Not supported by fastify. If you use fastify, you can exclude routes using regexp (set the renderPath to a regular expression) instead. | | serveStaticOptions | Object | Serve static options (static files) | | useGlobalPrefix | boolean | If true, static app will be prefixed by the global prefix set through setGlobalPrefix(). Default: false https://docs.nestjs.com/faq/global-prefix |

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.