A simple and flexible library for mocking APIs in Node.js and the browser. Define routes, generate dummy data, simulate authentication, and intercept fetch requests.
const express = require('express');
const Mockas = require('mockas');
const app = express();
const port = 3001;
const mockas = new Mockas();
// --- Define Mockas Routes ---
mockas.get('/api/status')
.willReturn({ status: 'ok', message: 'Mockas server is running!' })
.withStatus(200)
.register(); // IMPORTANT: Register the route with Mockas
mockas.post('/api/items')
.withHandler((requestData) => {
console.log('Received data on POST /api/items:', requestData);
// You can access the body data sent in the POST request
const newItemName = requestData.name || 'Unnamed Item';
// Return a dynamic response based on the input
return {
success: true,
message: "Item", newItemName, "created successfully.",
received_data: requestData
};
})
.withStatus(201)
.register();
// IMPORTANT: Use body-parsing middleware *before* Mocka middleware
// This parses incoming JSON request bodies and makes them available on "req.body"
app.use(express.json())
// Add the Mockas middleware
// Pass your Mockas instance to it.
// This middleware will intercept requests matching your defined Mockas routes.
app.use(Mockas.middleware(mockas));
// Optional: Fallback for routes not handled by Mockas
app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found by Mockas or Express.' });
});
app.listen(port, () => {
console.log('Express server with Mockas running at http://localhost:3000');
});
Mockas provides everything you need to simulate APIs and accelerate your development workflow.
Define routes and responses intuitively with a clean, chainable API.
Works server-side with Express and client-side by intercepting fetch requests.
Create dummy data (users, posts, etc.) based on schemas for realistic testing.
Add artificial latency to responses to test loading states and timeouts.
Simple setup for token-based authentication with Bearer tokens and login routes.
Easily integrate Mockas into Express/Connect applications as middleware.
Mockas intercepts network requests and provides mock responses based on your configuration. [^1][^2]
Create mock endpoints with path parameters, query parameters, and headers.
Mockas replaces window.fetch
in the browser to intercept requests. [^1]
Return fixed data or use handler functions for dynamic logic based on requests. [^2]
const express = require('express');
const Mockas = require('mockas');
const app = express();
const port = 3000;
const mockas = new Mockas({
// delay: 100 // Example: Global delay
});
mockas.get('/api/fast-ping')
.willReturn({ reply: 'PONG!', timestamp: new Date().toISOString() })
.withStatus(200)
.register(); // Register the route
mockas.get('/api/slow-data')
.willReturn({
message: "This data took a while to load!",
data: [
{ id: 'abc', value: 123 },
{ id: 'def', value: 456 }
]
})
.withStatus(200)
.withDelay(2500) // <--- Add a 2500 millisecond (2.5 second) delay
.register();
app.use(express.json());
// Add the Mockas middleware
// Pass your Mockas instance to it.
// Use the renamed static middleware property
app.use(Mockas.middleware(mockas));
// Optional: Fallback for routes not handled by Mockas
app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found by Mockas or Express.' });
});
app.listen(port, () => {
console.log('Express server with Mockas running at http://localhost:3000');
});
npm install @skielder/mockas
# or
yarn add @skielder/mockas
Mockas is proudly open source and maintained by a community of developers passionate about building better tools.
Join developers who use Mockas to streamline their API development workflow.