Production-grade security + performance toolkit for backend frameworks with OWASP Top 10 compliance
Secure Backend is a comprehensive middleware solution that provides enterprise-level security and performance optimizations for Express, Koa, Fastify, and NestJS applications. Get OWASP Top 10 protection, performance monitoring, and developer-friendly configuration in minutes, not hours.
npm install secure-backend
import express from 'express';
import { ExpressAdapter, secureBackend } from 'secure-backend';
const app = express();
// One-line security + performance setup
const secureAdapter = new ExpressAdapter(secureBackend('api'));
secureAdapter.applyMiddleware(app);
app.listen(3000);
That's it! Your app now has:
Building secure, performant backend APIs requires implementing dozens of security measures and performance optimizations. Most developers either:
Secure Backend gives you enterprise-grade security + performance with zero configuration complexity.
# npm
npm install secure-backend
# yarn
yarn add secure-backend
# pnpm
pnpm add secure-backend
# Express.js
npm install secure-backend express
# Koa.js
npm install secure-backend koa
# Fastify
npm install secure-backend fastify
# NestJS
npm install secure-backend @nestjs/common @nestjs/core
import { secureBackend } from 'secure-backend';
const config = secureBackend('api');
// Optimized for: REST APIs, microservices, mobile backends
// Features: Strong CSRF, strict CORS, API-focused headers
const config = secureBackend('webapp');
// Optimized for: Server-rendered web apps, full-stack apps
// Features: Relaxed CORS, web-friendly CSP, session support
const config = secureBackend('strict');
// Optimized for: Financial, healthcare, high-security apps
// Features: Maximum security headers, strict rate limits, enhanced monitoring
import express from 'express';
import { ExpressAdapter, secureBackend } from 'secure-backend';
const app = express();
const secureAdapter = new ExpressAdapter(secureBackend('api'));
secureAdapter.applyMiddleware(app);
app.get('/api/users', (req, res) => {
res.json({ users: [] });
});
app.listen(3000);
import Koa from 'koa';
import { KoaAdapter, secureBackend } from 'secure-backend';
const app = new Koa();
const secureAdapter = new KoaAdapter(secureBackend('webapp'));
secureAdapter.applyMiddleware(app);
app.use(async ctx => {
ctx.body = { message: 'Secure Koa app!' };
});
app.listen(3000);
import Fastify from 'fastify';
import { FastifyAdapter, secureBackend } from 'secure-backend';
const fastify = Fastify();
const secureAdapter = new FastifyAdapter(secureBackend('strict'));
await secureAdapter.applyMiddleware(fastify);
fastify.get('/api/health', async () => {
return { status: 'healthy' };
});
await fastify.listen({ port: 3000 });
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SecureBackendMiddleware, secureBackend } from 'secure-backend';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(new SecureBackendMiddleware(secureBackend('api')));
await app.listen(3000);
}
bootstrap();
import { secureBackend, SecureBackendConfig } from 'secure-backend';
const config: SecureBackendConfig = secureBackend({
preset: 'api',
security: {
cors: {
origin: ['https://myapp.com', 'https://admin.myapp.com'],
credentials: true,
},
csrf: {
enabled: true,
tokenLength: 32,
cookieName: 'csrf-token',
},
rateLimit: {
enabled: true,
max: 1000,
windowMs: 15 * 60 * 1000, // 15 minutes
},
headers: {
csp: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
},
},
sanitization: {
enabled: true,
xss: true,
sqlInjection: true,
},
auth: {
jwt: {
secret: process.env.JWT_SECRET,
algorithms: ['HS256'],
maxAge: 3600,
},
},
},
performance: {
compression: {
enabled: true,
threshold: 1024,
level: 6,
},
caching: {
enabled: true,
maxAge: 300,
etag: true,
},
monitoring: {
enabled: true,
logSlowRequests: true,
slowRequestThreshold: 1000,
},
},
logging: {
enabled: true,
level: 'info',
suspiciousRequests: true,
rateLimitViolations: true,
},
});
// Automatically applied based on preset
{
contentSecurityPolicy: true,
crossOriginEmbedderPolicy: true,
crossOriginOpenerPolicy: true,
crossOriginResourcePolicy: true,
dnsPrefetchControl: true,
frameguard: true,
hidePoweredBy: true,
hsts: true,
ieNoOpen: true,
noSniff: true,
originAgentCluster: true,
permittedCrossDomainPolicies: true,
referrerPolicy: true,
xssFilter: true,
}
/performance-metrics
)// Access performance data
app.get('/metrics', (req, res) => {
const metrics = secureAdapter.getPerformanceMetrics();
res.json(metrics);
});
Generate secure configuration templates:
# Initialize with recommended defaults
npx secure-backend init
# Choose preset
npx secure-backend init --preset=api
npx secure-backend init --preset=webapp
npx secure-backend init --preset=strict
# Generate for specific framework
npx secure-backend init --framework=express
npx secure-backend init --framework=koa
npx secure-backend init --framework=fastify
Secure Backend automatically adds helpful development endpoints:
GET /performance-metrics
- Performance and timing dataGET /security-events
- Security violation logsGET /config-summary
- Current configuration overviewGET /csrf-token
- CSRF token for frontend appsNote: These endpoints are automatically disabled in production.
# Clone repository
git clone https://github.com/secure-backend/secure-backend.git
cd secure-backend
# Install dependencies
npm install
# Run tests
npm test
# Build package
npm run build
# Run examples
npm run example:express
npm run example:koa
npm run example:fastify
We welcome contributions! Please see our Contributing Guide for details.
MIT License - see LICENSE file for details.
Made with ❤️ by the Secure Backend team
Secure Backend is trusted by companies building production applications. Join thousands of developers who've chosen security by default.