Steps to create Express application

Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal.

mkdir folder-name
cd folder-name

Step 2: After creating the folder, initialize the NPM using the below command. Using this the package.json file will be created.

npm init -y

Step 3: Now, we will install the express dependency for our project using the below command.

npm i express

Step 4: Now create the below Project Structure of our project which includes the file as app.js and public directory in which images are stored.

Project Structure:

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2"
}

How to set response header on Express JS assets?

In Express.js applications, we can set the response headers on assets to improve performance and also control the cache behavior.

In this article, we will set the response header on Express.js assets by using 2 different approaches. We will see the practical implementation of this approach in terms of examples and output.

The following approaches will be used to set response header in Express

Table of Content

  • Using Middleware
  • Using Route Handler Directly

Similar Reads

Prerequisites

Node JS Express JS...

Steps to create Express application

Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal....

Approach 1: Using Middleware

In this approach, we have defined the middleware before serving the static assets. When the request to the asset is done, it will go through the middleware in which we have set the response headers. In the below example, we have set the response header as “public, max-age=3600“, which specified the cache policy allowing the public caching for up to 3600 seconds....

Approach 2: Using Route Handler Directly

...