Skip to content

Instantly share code, notes, and snippets.

@p3nj
Last active March 23, 2025 22:29
Show Gist options
  • Save p3nj/e7c8ec5258d1b1eb8df9d0e9ff75a85e to your computer and use it in GitHub Desktop.
Save p3nj/e7c8ec5258d1b1eb8df9d0e9ff75a85e to your computer and use it in GitHub Desktop.
An useless API
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON and form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Default timeout duration in milliseconds
const DEFAULT_TIMEOUT = 5000; // 5 seconds
// Root route with basic information
app.get('/', (req, res) => {
res.status(200).json({
title: "Error Simulation API",
availableEndpoints: [
"/api/error/524 - Gateway Timeout errors",
"/api/error/500 - Internal Server errors",
"/api/error/503 - Service Unavailable errors",
"/api/error/400 - Bad Request errors",
"/api/error/404 - Not Found errors",
"/api/success/200 - Success response",
"/api/custom?code=xxx - Custom status code response"
],
usage: "Add ?timeout=milliseconds to control response delay"
});
});
// Helper function to get timeout from query params or request body
const getTimeoutDuration = (req) => {
const timeoutFromQuery = req.query.timeout;
const timeoutFromBody = req.body && req.body.timeout;
return timeoutFromQuery ? parseInt(timeoutFromQuery)
: timeoutFromBody ? parseInt(timeoutFromBody)
: DEFAULT_TIMEOUT;
};
// 524 Gateway Timeout route
app.all('/api/error/524', (req, res) => {
const timeoutDuration = getTimeoutDuration(req);
console.log(`${req.method} request to ${req.path}: 524 Error will be sent after ${timeoutDuration}ms`);
setTimeout(() => {
res.status(524).json({
error: "Gateway Timeout",
code: 524,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The server timed out while processing your request"
}
]
}
});
}, timeoutDuration);
});
// 500 Internal Server Error route
app.all('/api/error/500', (req, res) => {
const timeoutDuration = getTimeoutDuration(req);
console.log(`${req.method} request to ${req.path}: 500 Error will be sent after ${timeoutDuration}ms`);
setTimeout(() => {
res.status(500).json({
error: "Internal Server Error",
code: 500,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The server encountered an unexpected condition that prevented it from fulfilling the request"
}
]
}
});
}, timeoutDuration);
});
// 503 Service Unavailable route
app.all('/api/error/503', (req, res) => {
const timeoutDuration = getTimeoutDuration(req);
console.log(`${req.method} request to ${req.path}: 503 Error will be sent after ${timeoutDuration}ms`);
setTimeout(() => {
res.status(503).json({
error: "Service Unavailable",
code: 503,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The server is not ready to handle the request"
}
]
}
});
}, timeoutDuration);
});
// 400 Bad Request route
app.all('/api/error/400', (req, res) => {
res.status(400).json({
error: "Bad Request",
code: 400,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The server could not understand the request due to invalid syntax"
}
]
}
});
});
// 404 Not Found route
app.all('/api/error/404', (req, res) => {
res.status(404).json({
error: "Not Found",
code: 404,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The requested resource was not found"
}
]
}
});
});
// 200 Success route
app.all('/api/success/200', (req, res) => {
const timeoutDuration = getTimeoutDuration(req);
console.log(`${req.method} request to ${req.path}: 200 Success will be sent after ${timeoutDuration}ms`);
setTimeout(() => {
res.status(200).json({
status: "Success",
code: 200,
path: req.originalUrl,
results: {
messages: [
{
code: "S",
message: "The request was processed successfully"
}
]
}
});
}, timeoutDuration);
});
// Custom status code route
app.all('/api/custom', (req, res) => {
// Get parameters from query params or request body
const statusCode = parseInt(req.query.code || (req.body && req.body.code)) || 200;
const message = req.query.message || (req.body && req.body.message) || "Custom response message";
const messageCode = req.query.messageCode || (req.body && req.body.messageCode) || (statusCode >= 400 ? "E" : "S");
const timeoutDuration = getTimeoutDuration(req);
console.log(`${req.method} request to ${req.path}: Custom response ${statusCode} will be sent after ${timeoutDuration}ms`);
setTimeout(() => {
// Determine if this is an error or success based on status code
const isError = statusCode >= 400;
res.status(statusCode).json({
[isError ? "error" : "status"]: isError ? "Custom Error" : "Custom Success",
code: statusCode,
path: req.originalUrl,
results: {
messages: [
{
code: messageCode,
message: message
}
]
}
});
}, timeoutDuration);
});
// Catch-all for any other routes to return 404
app.use((req, res) => {
res.status(404).json({
error: "Not Found",
code: 404,
path: req.originalUrl,
results: {
messages: [
{
code: "E",
message: "The requested resource was not found"
}
]
}
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: "Internal Server Error",
code: 500,
results: {
messages: [
{
code: "E",
message: "An unexpected error occurred on the server"
}
]
}
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Error Simulation API server running on port ${PORT}`);
console.log(`Default timeout for delayed responses: ${DEFAULT_TIMEOUT/1000} seconds`);
console.log(`Customize response delay with ?timeout=milliseconds in the URL`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment