@morojs/moro / builtInMiddleware
Variable: builtInMiddleware
constbuiltInMiddleware:object
Defined in: src/core/middleware/built-in/index.ts:88
Type Declaration
auth()
auth: (
options) =>MiddlewareInterface
Auth hook for global usage Registers with the hooks system for application-wide Better Auth authentication
Parameters
options
Returns
Example
import { auth, providers } from '@/middleware/built-in/auth'; app.use(auth({ secret: process.env.AUTH_SECRET, providers: [ providers.google({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET }), providers.github({ clientId: process.env.GITHUB_CLIENT_ID, clientSecret: process.env.GITHUB_CLIENT_SECRET }) ], session: { strategy: 'jwt', maxAge: 30 * 24 * 60 * 60 } }));
bodySize()
bodySize: (
options) =>Middleware
Create body size limit middleware
Parameters
options
BodySizeOptions = {}
Returns
Example
import { bodySize } from '@morojs/moro'; app.use(bodySize({ limit: '10mb' })); app.use(bodySize({ limit: 1024 * 1024 * 5 })); // 5MB
cache()
cache: (
options) =>MiddlewareInterface
Advanced cache hook with pluggable storage adapters and HTTP caching Registers with the hooks system for global usage
Parameters
options
CacheOptions = {}
Returns
Example
import { cache } from '@/middleware/built-in/cache'; // Server-side caching with Redis app.use(cache({ adapter: 'redis', defaultTtl: 3600, strategies: { '/api/.*': { ttl: 60 } } })); // HTTP caching with ETags and conditional requests app.use(cache({ httpCaching: true, maxAge: 300, etag: 'strong', conditionalRequests: true })); // Combined server-side + HTTP caching app.use(cache({ adapter: 'memory', defaultTtl: 3600, httpCaching: true, maxAge: 300, strategies: { '/api/users': { ttl: 300, maxAge: 60 }, '/api/posts': { ttl: 1800, maxAge: 300 } } })); // Redis server-side caching only app.use(cache({ adapter: 'redis', defaultTtl: 3600, httpCaching: false, // Disable HTTP caching strategies: { '/api/.*': { ttl: 60 } } })); // HTTP caching only (no server-side storage) app.use(cache({ httpCaching: true, maxAge: 300, etag: 'strong', vary: ['Accept-Language', 'User-Agent'] }));
cdn()
cdn: (
options) =>MiddlewareInterface
CDN hook for global usage Registers with the hooks system for application-wide CDN support
Parameters
options
CDNOptions = {}
Returns
Example
import { cdn } from '@/middleware/built-in/cdn'; app.use(cdn({ adapter: 'cloudflare', adapterOptions: { zoneId: 'your-zone-id', apiToken: 'your-api-token' }, autoInvalidate: true, invalidationPatterns: ['/api/.*'] }));
compression()
compression: (
options) =>Middleware
Create compression middleware
Parameters
options
CompressionOptions = {}
Returns
Example
import { compression } from '@morojs/moro'; app.use(compression({ threshold: 1024, // Only compress responses > 1KB level: 6, // Compression level (0-9) filter: (req, res) => { // Don't compress images return !req.path.match(/\.(jpg|jpeg|png|gif)$/); }, }));
cookie()
cookie: (
_config) =>MiddlewareInterface
Cookie hook for global usage Registers with the hooks system for application-wide cookie handling
Parameters
_config
CookieConfig = {}
Returns
Example
import { cookie } from '@/middleware/built-in/cookie'; app.use(cookie({ secret: 'my-secret-key', signed: true }));
cors()
cors: (
options) =>MiddlewareInterface
CORS hook for global usage Registers with the hooks system for application-wide CORS
Parameters
options
CORSOptions = {}
Returns
Example
import { cors } from '@/middleware/built-in/cors'; app.use(cors({ origin: 'https://example.com', credentials: true }));
csp()
csp: (
options) =>MiddlewareInterface
CSP hook for global usage Registers with the hooks system for application-wide Content Security Policy
Parameters
options
CSPOptions = {}
Returns
Example
import { csp } from '@/middleware/built-in/csp'; app.use(csp({ directives: { defaultSrc: ["'self'"], scriptSrc: ["'self'", 'https://cdn.example.com'], styleSrc: ["'self'", "'unsafe-inline'"] }, nonce: true, reportUri: '/csp-report' }));
csrf()
csrf: (
options) =>MiddlewareInterface
CSRF hook for global usage Registers with the hooks system for application-wide CSRF protection
Parameters
options
CSRFOptions = {}
Returns
Example
import { csrf } from '@/middleware/built-in/csrf'; app.use(csrf({ cookieName: '_csrf', ignoreMethods: ['GET', 'HEAD', 'OPTIONS'] }));
graphql()
graphql: (
options) =>MiddlewareInterface
GraphQL Hook Middleware Provides hook-based GraphQL integration with MoroJS middleware system
Parameters
options
Returns
helmet()
helmet: (
options) =>Middleware
Create Helmet security headers middleware
Parameters
options
HelmetOptions = {}
Returns
Example
import { helmet } from '@morojs/moro'; app.use(helmet({ contentSecurityPolicy: { defaultSrc: ["'self'"], scriptSrc: ["'self'", "'unsafe-inline'"], }, strictTransportSecurity: { maxAge: 31536000, includeSubDomains: true, }, }));
http2
http2:
object
http2.push()
push: (
options) =>Middleware=createHttp2PushMiddleware
Create HTTP/2 Server Push middleware
Parameters
options
Http2PushOptions = {}
Returns
Example
import { http2 } from '@morojs/moro'; app.use(http2.push({ autoDetect: true, resources: [ { path: '/styles/main.css', as: 'style', type: 'text/css', priority: 200 }, { path: '/scripts/app.js', as: 'script', type: 'application/javascript', priority: 150 }, ], condition: (req) => req.path === '/' || req.path.endsWith('.html'), }));
range()
range: (
options) =>Middleware
Create HTTP range requests middleware
Parameters
options
RangeOptions = {}
Returns
Example
import { range } from '@morojs/moro'; app.use(range({ acceptRanges: 'bytes', maxRanges: 1, })); app.get('/video/:file', async (req, res) => { const filePath = `./videos/${req.params.file}`; await res.sendRange(filePath); });
rateLimit()
rateLimit: (
options) =>MiddlewareInterface
Rate limit hook for global usage Registers with the hooks system for application-wide rate limiting
Parameters
options
max?
number
message?
string
windowMs?
number
Returns
Example
import { rateLimit } from '@/middleware/built-in/rate-limit'; app.use(rateLimit({ windowMs: 60000, // 1 minute max: 100 // 100 requests per window }));
session()
session: (
options) =>MiddlewareInterface
Session hook for global usage Registers with the hooks system for application-wide session management
Parameters
options
SessionOptions = {}
Returns
Example
import { session } from '@/middleware/built-in/session'; app.use(session({ store: 'redis', cookie: { maxAge: 86400000 } }));
sse()
sse: (
options) =>MiddlewareInterface
SSE hook for global usage Registers with the hooks system for application-wide Server-Sent Events support
Parameters
options
SSEOptions = {}
Returns
Example
import { sse } from '@/middleware/built-in/sse'; app.use(sse({ heartbeat: 30000, retry: 3000, cors: true }));
staticFiles()
staticFiles: (
options) =>Middleware
Create static file serving middleware
Parameters
options
StaticOptions
Returns
Example
import { staticFiles } from '@morojs/moro'; app.use(staticFiles({ root: './public', maxAge: 3600, // 1 hour cache index: ['index.html'], etag: true, }));
template()
template: (
options) =>Middleware
Create template rendering middleware
Parameters
options
TemplateOptions
Returns
Example
import { template } from '@morojs/moro'; app.use(template({ views: './views', cache: true, defaultLayout: 'main', })); app.get('/', (req, res) => { res.render('index', { title: 'Home', user: { name: 'John' } }); });
upload()
upload: (
options) =>Middleware
Create file upload middleware
Parameters
options
UploadOptions = {}
Returns
Example
import { upload } from '@morojs/moro'; app.use(upload({ dest: './uploads', maxFileSize: 5 * 1024 * 1024, // 5MB maxFiles: 10, allowedTypes: ['image/jpeg', 'image/png', 'image/gif'], }));
validation()
validation: () =>
MiddlewareInterface
Basic validation hook for global usage Registers with the hooks system for content-type checking
Returns
Example
import { validation } from '@/middleware/built-in/validation'; app.use(validation());