• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Tutorials
      • Node.js Tutorial
      • Express.js Tutorial
      • Python Tutorial
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements

Serving Static Files in Express: A Complete Tutorial

  • June 23, 2023
  • CODE OF GEEKS
  • 0
In this Tutorial ->
  • Why Serve Static Files in Express?
  • Setting Up Express Application
  • Configuring Static File Serving
  • Organizing Static Assets
  • Caching and Compression
Serving Static Files in Express: A Complete Tutorial
Serving Static Files in Express: A Complete Tutorial

Serving static files efficiently is crucial for web applications. In this tutorial, we’ll explore how to serve static files in Express and optimize them for performance.

Why Serve Static Files in Express?

Serving static files, such as CSS, JavaScript, and images, from Express improves performance by reducing server load and enhancing user experience. Additionally, optimizing static files for SEO ensures better search engine visibility.



Setting Up Express Application

Initialize a new Express application by following these steps:

mkdir my-app
cd my-app
npm init -y
npm install express

Create an app.js file and add the following code:

const express = require('express');
const app = express();
const port = 3000;

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Configuring Static File Serving

To serve static files, create a public directory in your project and place your static assets inside it. Configure Express to serve these files using the express.static() middleware.

Update your app.js file:

const path = require('path');

// Serve static files
app.use(express.static(path.join(__dirname, 'public')));

Now, any file placed in the public directory can be accessed directly from the browser.

Organizing Static Assets

Organize your static assets into meaningful folders within the public directory. For example, place CSS files in a css folder, JavaScript files in a js folder, and images in an images folder.

public/
  css/
    styles.css
  js/
    main.js
  images/
    logo.png


Caching and Compression

Implement caching and compression to improve performance. Add the following lines to your app.js file:

// Enable caching
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));

// Enable compression
app.use(compression());

The maxAge option sets the caching duration in milliseconds (here, it’s set to one year). The compression() middleware enables Gzip compression for static assets.

By following this tutorial, you have learned how to serve static files in Express. You also explored optimization techniques for better performance. Apply these practices to deliver static assets efficiently and enhance your web application’s overall experience.





Keywords to search: serving static files, Express static file serving, static file optimization, static file caching, static file compression, SEO for static files, performance optimization, web application assets, static file delivery,

Tags: Express static file servingperformance optimizationSEO for static filesserving static filesstatic file cachingstatic file compressionstatic file deliverystatic file optimizationweb application assets
  • Previous Handling Forms with Express: A Comprehensive Tutorial
  • Next Building Dynamic Web Applications with Express and EJS: A Step-by-Step Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Advertise with us

More on Node.js

  • Introduction to Node.js
  • NPM in Node.js
  • Non-Blocking Nature of Node.js
  • First Application with Node.js
  • Node.JS Modules
  • HTTP Module Node.js
  • Reading File with Node.js
  • Writing File with Node.js
  • Creating File with Node.js
  • Updating File with Node.js
  • Deleting File with Node.js
  • Renaming File with Node.js
  • URL Module in Node.js
  • Event Programming with Node.js
  • async-await in Node.js
  • Buffer in Node.js
  • Sending Mails with Node.js
  • Connecting to MySQL(CRUD) in Node.js

More on Express.js

  • Introduction to Express.js
  • Creating First App with Express.js
  • Routes in Express.js
  • Middlewares in Express.js
  • Web App with Express & Pug
  • Web App with Express & EJS
  • Serving Static Files with Express.js
  • Handling Forms with Express.js
  • Handling File uploads in forms
  • Working with Cookies in Express.js
  • Session Handling with Express.js
  • REST API with Express.js
  • Validating Requests in Express.js
  • Fetch data from External API with axios
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions
Refund Policy
Contact us

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Tutorials

Python
Node.js
Express.js
Golang

Recent Posts

Strings in Go and its basic operations
  • August 11, 2023
Introduction to Functions in Go programming
  • August 11, 2023

Copyright @ CODE OF GEEKS. All Rights Reserved.