Circle Menu
Dialog Form
Dominoes List Scroll
Dominoes Scroll Indicator
Eagle Vision
Electric AI Input
File Input
Flip Scroll
Glowing Scroll Indicator
Horizontal Scroll
Icon Wheel
Image Pile
Interactive CTA
Interactive Folder
Interest Picker
Jelly Loader
Leave Rating
Mask Cursor Effect
Magnet Tabs
Masonry Grid
OTP Input
Photo Gallery
Pixelated Carousel
Rolling Ball Scroll Indicator
Rubik Cube
Sidebar
Sine Wave
Skeumorphic Music Card
Social Media Card
Stacked Input Form
Stack Scroll
Trading Card
Express Server
This Express.js server setup includes CORS configuration, rate limiting for security, JSON parsing, and essential middleware for handling requests efficiently. It follows best practices for maintainability, security, and flexibility, making it easy to scale and integrate with a frontend application.
Install dependencies
npm i express cors express-rate-limit
Express Server.js Template
import express from 'express';
import cors from 'cors';
import rateLimit from 'express-rate-limit';
import './config/logging'; // ? Custom logging setup, refer to the logging guide -> https://stackbits.dev/docs/customlogger
const PORT = process.env.PORT || 3000; // Use environment variable for flexibility
const app = express();
// Middleware Configuration
// Enable CORS for specific origins
app.use(
cors({
origin: ['http://localhost:3000'], // Adjust based on frontend deployment
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // Explicitly allow methods
})
);
// Rate limiting to prevent abuse
const limiter = rateLimit({
windowMs: 60 * 1000, // 1-minute window
limit: 100, // Max 100 requests per window
standardHeaders: 'draft-7', // Use the latest draft headers
legacyHeaders: false, // Disable deprecated headers
message: { error: 'Too many requests from this IP, please try again later.' }, // Consistent error format
});
app.use(limiter);
// Parsing middleware
app.use(express.json({ limit: '20mb' })); // Set a reasonable body size limit
app.use(express.urlencoded({ extended: false })); // Enable URL-encoded data parsing
// Additional security headers
app.use((req, res, next) => {
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
// * Define API Routes here
// Example: app.use('/api/users', userRoutes);
// Start the server
app.listen(PORT, () => {
console.info(`✅ Server is running on http://localhost:${PORT}`);
});