initStaticResourceMiddleware(options)
The initStaticResourceMiddleware is a powerful Express middleware designed to serve static assets from other installed blocklets. This allows your application to access shared resources like images, stylesheets, or fonts from dependent components without needing to know their exact location on the file system.
The middleware works by scanning the directories of installed blocklets that match a specified resource type, creating an in-memory map of available files. When a request comes in, it efficiently looks up the file in this map and serves it.
How It Works#
Here’s a high-level overview of the process:
Usage#
To use the middleware, import it and add it to your Express application. You need to configure which resource types it should look for.
server.js
import express from 'express';
import { initStaticResourceMiddleware } from '@blocklet/uploader-server';
const app = express();
// Initialize the middleware to serve resources of type 'imgpack'
// from any installed blocklet that provides it.
app.use(
initStaticResourceMiddleware({
express,
resourceTypes: ['imgpack'], // Simple configuration using a string
})
);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});In this example, if another installed blocklet has a blocklet.yml entry for a resource of type imgpack, any file within that resource's directory will be served. For example, a request to /example.png would serve the example.png file from that blocklet.
Options#
The initStaticResourceMiddleware function accepts a configuration object with the following properties:
Option | Type | Description |
|---|---|---|
|
| Required. The Express application instance. |
|
| Required. An array defining the resource types to scan. See the |
|
| Optional. A configuration object passed to the underlying |
|
| Optional. If |
The ResourceType Object#
For more granular control, you can provide an array of objects to the resourceTypes option instead of simple strings. Each object can have the following properties:
Property | Type | Description |
|---|---|---|
|
| Required. The name of the resource type, which should match the type defined in a dependent blocklet's |
|
| Required. The DID of the blocklet component that provides the resource. You can use |
|
| Optional. A specific sub-folder or an array of sub-folders within the resource directory to scan. Defaults to the root ( |
|
| Optional. An array of file extensions to include (e.g., |
|
| Optional. An array of file extensions to exclude (e.g., |
|
| Optional. A function to set custom response headers for a served file. |
|
| Optional. Overrides the top-level |
|
| Optional. Overrides the top-level |
Advanced Example#
This example demonstrates a more complex configuration serving two different types of resources with specific rules.
server.js
import express from 'express';
import { initStaticResourceMiddleware } from '@blocklet/uploader-server';
import { ImageBinDid } from '@blocklet/uploader-server/constants';
const app = express();
app.use(
initStaticResourceMiddleware({
express,
skipRunningCheck: true,
resourceTypes: [
{
type: 'imgpack',
did: ImageBinDid,
folder: 'public/images',
whitelist: ['.png', '.jpg', '.gif'],
},
{
type: 'theme-assets',
did: 'z2q...someThemeBlockletDid', // DID of a specific theme blocklet
folder: ['css', 'fonts'],
blacklist: ['.map'],
},
],
options: {See all 6 lines
This configuration does the following:
- Scans for
imgpackresources provided by the Media Kit (ImageBinDid), but only within thepublic/imagessub-folder, and only serves.png,.jpg, and.giffiles. - Scans for
theme-assetsresources from a blocklet with a specific DID, looking in both thecssandfontssub-folders, and ignores any source map (.map) files. - Sets a default
Cache-Controlmax-age of 7 days for all matched files.
Automatic Updates#
This middleware is designed for a dynamic environment. It automatically listens for blocklet lifecycle events. If a component is added, removed, started, stopped, or updated, the middleware will automatically rescan and update its internal resource map, so you don't need to restart your application.
Next, learn how to serve files from a directory that can be updated in real-time without requiring an application restart.