UNPKG

api-scout

Version:

🔍 Automatically scout, discover and generate beautiful interactive API documentation from your codebase. Supports Express.js, NestJS, FastAPI, Spring Boot with interactive testing and security analysis.

231 lines (199 loc) 5.02 kB
const express = require('express'); const app = express(); // Middleware app.use(express.json()); /** * Get all users * Returns a list of all users in the system */ app.get('/api/users', (req, res) => { const { page = 1, limit = 10, search } = req.query; res.json({ users: [ { id: 1, name: 'John Doe', email: 'john@example.com' }, { id: 2, name: 'Jane Smith', email: 'jane@example.com' } ], pagination: { page: parseInt(page), limit: parseInt(limit), total: 2 } }); }); /** * Get user by ID * Retrieves a specific user by their unique identifier */ app.get('/api/users/:id', (req, res) => { const { id } = req.params; if (!id) { return res.status(400).json({ error: 'User ID is required' }); } res.json({ id: parseInt(id), name: 'John Doe', email: 'john@example.com', profile: { avatar: 'https://example.com/avatar.jpg', bio: 'Software developer' } }); }); /** * Create new user * Creates a new user account with the provided information */ app.post('/api/users', authenticateToken, validateUser, (req, res) => { const { name, email, password } = req.body; const newUser = { id: Date.now(), name, email, createdAt: new Date().toISOString() }; res.status(201).json({ message: 'User created successfully', user: newUser }); }); /** * Update user * Updates an existing user's information */ app.put('/api/users/:id', authenticateToken, (req, res) => { const { id } = req.params; const { name, email } = req.body; res.json({ id: parseInt(id), name, email, updatedAt: new Date().toISOString() }); }); /** * Delete user * Permanently removes a user from the system */ app.delete('/api/users/:id', authenticateToken, adminOnly, (req, res) => { const { id } = req.params; res.json({ message: `User ${id} has been deleted`, deletedAt: new Date().toISOString() }); }); /** * Get user posts * Retrieves all posts created by a specific user */ app.get('/api/users/:id/posts', (req, res) => { const { id } = req.params; const { status = 'published' } = req.query; res.json({ posts: [ { id: 1, title: 'Hello World', content: 'This is my first post', status, createdAt: '2023-01-01T00:00:00Z' } ], totalCount: 1 }); }); // Authentication middleware function authenticateToken(req, res, next) { const authHeader = req.headers['authorization']; const token = authHeader && authHeader.split(' ')[1]; if (!token) { return res.status(401).json({ error: 'Access token required' }); } // In real app, verify JWT token next(); } // Validation middleware function validateUser(req, res, next) { const { name, email } = req.body; if (!name || !email) { return res.status(400).json({ error: 'Name and email are required', required: ['name', 'email'] }); } next(); } // Admin authorization middleware function adminOnly(req, res, next) { // In real app, check user role const isAdmin = req.headers['x-user-role'] === 'admin'; if (!isAdmin) { return res.status(403).json({ error: 'Admin access required' }); } next(); } // Products API /** * Get all products * Returns a paginated list of products with optional filtering */ app.get('/api/products', (req, res) => { const { category, priceMin, priceMax, sort = 'name' } = req.query; res.json({ products: [ { id: 1, name: 'Laptop', description: 'High-performance laptop', price: 999.99, category: 'electronics', inStock: true }, { id: 2, name: 'Book', description: 'Programming guide', price: 29.99, category: 'books', inStock: true } ], filters: { category, priceMin, priceMax }, sorting: sort }); }); /** * Create product * Adds a new product to the catalog */ app.post('/api/products', authenticateToken, (req, res) => { const { name, description, price, category } = req.body; res.status(201).json({ id: Date.now(), name, description, price: parseFloat(price), category, createdAt: new Date().toISOString() }); }); // Error handling middleware app.use((err, req, res, next) => { console.error(err.stack); res.status(500).json({ error: 'Something went wrong!', message: err.message }); }); // 404 handler app.use('*', (req, res) => { res.status(404).json({ error: 'Route not found', path: req.originalUrl, method: req.method }); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); module.exports = app;