Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する

Middleware Reference


Blocklet SDK provides a comprehensive suite of Express.js middleware, designed to streamline common web development tasks within your Blocklet applications. These middlewares facilitate crucial functionalities such as user session management, authentication, protection against common web vulnerabilities like CSRF, verification of inter-Blocklet and inter-component communications, dynamic sitemap generation for SEO, and robust fallback HTML serving for single-page applications.

Each middleware is designed to integrate seamlessly into your Express.js application, providing a modular approach to adding powerful features. For detailed information on specific middlewares, please refer to their dedicated sections:

Overview of Middlewares#

The following table provides a quick overview of the middlewares available in the Blocklet SDK:

Middleware

Description

Detailed Documentation

session

Manages user sessions, login tokens, and access keys for request authentication.

Authentication

auth

Enforces authorization based on user roles, permissions, KYC status, and authentication methods.

Authentication

csrf

Protects your web application against Cross-Site Request Forgery (CSRF) attacks.

CSRF Protection

component, verifyBlockletSig

Verifies digital signatures on requests originating from other Blocklets or components within the Blocklet Server ecosystem.

Blocklet/Component Signature

sitemap

Generates dynamic sitemap XML files to improve search engine optimization for your Blocklet.

Sitemap Generation

fallback

Serves a fallback HTML page for single-page applications, ensuring proper SEO and initial page load.

Fallback HTML

Blocklet SDK Middlewares

Authentication

Session Middleware

Auth Middleware

Security & Verification

CSRF Protection

Blocklet/Component Signature

Content & SEO

Sitemap Generation

Fallback HTML


Integrating Middlewares#

Integrating Blocklet SDK middlewares into your Express.js application is straightforward. You can apply them globally or to specific routes, depending on your application's needs.

Here is a basic example demonstrating how to set up some of the key middlewares in an Express.js application:

import express from 'express';
import { auth, session, csrf, fallback } from '@blocklet/sdk/middlewares';
import { resolve } from 'path';

const app = express();

// Use session middleware to authenticate incoming requests based on various tokens
app.use(session());

// Use CSRF protection for POST, PUT, PATCH, DELETE requests
app.use(csrf());

// Example route protected by auth middleware, requiring an 'admin' role
app.get('/admin', auth({ roles: ['admin'] }), (req, res) => {
res.send('Welcome to the admin dashboard!');
});

// Serve static assets from the 'dist' directory
app.use(express.static(resolve(__dirname, '../dist')));

// Use fallback middleware to serve index.html for all unhandled routes,
// injecting dynamic page data for SEO purposes.
app.use(fallback(resolve(__dirname, '../dist/index.html'), {
getPageData: async (req) => ({
title: 'My Blocklet Application',
description: 'A decentralized application powered by ArcBlock Blocklet Server.',
ogImage: 'https://your-blocklet-url.com/blocklet/og.png' // Example Open Graph image
}),
// Optional: configure cache TTL for the fallback response
cacheTtl: 5 * 60 * 1000 // Cache for 5 minutes
}));

const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Blocklet application listening on port ${port}`);
});

This example demonstrates how to apply session and csrf globally, apply auth to a specific route, and use fallback to handle single-page application routing and SEO.


This section provided an overview of the Express.js middlewares available in the Blocklet SDK. You can now proceed to explore each middleware in detail to understand its specific functionalities and configuration options. Continue your journey by diving into Authentication middleware details.