1. Use Case: User this delete api Code: const deleteTodoTask = async (req, res) =>…
- User
User: 1. Use Case: User this delete api Code: const deleteTodoTask = async (req, res) => { const { todoId } = req.params try { // Validate the input if (!todoId) { return { success: false, message: 'Todo ID must be provided.' }; }
// Find and delete the todo by ID const deletedTodo = await Todo.findByIdAndDelete(todoId); // Check if the todo was found and deleted if (!deletedTodo) { return { success: false, message: 'Todo not found.' }; } // Success response return { success: true, message: 'Todo deleted successfully.' }; } catch (error) { // Handle any errors that occur during the process console.error('Error deleting todo:', error); return { success: false, message: 'An error occurred while deleting the todo.' }; }}; User: Description: 1. Install necessary packages: express, helmet, cors, rate-limiter., 2. Set up the server using Express., 3. Configure helmet for security enhancements., 4. Enable cors for universal access., 5. Implement rate-limiter middleware for request rate limiting., 6. Define routes and attach error middleware for handling errors., 7. Set the server to listen on port 4000.
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only.Assistant:
- Assistant
{ "name": "todo_api_server", "schema": { "type": "string", "code": "const express = require('express');\nconst helmet = require('helmet');\nconst cors = require('cors');\nconst rateLimit = require('express-rate-limit');\nconst mongoose = require('mongoose');\n\nconst app = express();\nconst PORT = 4000;\n\n// Connect to MongoDB\nmongoose.connect('mongodb://localhost:27017/todo', { useNewUrlParser: true, useUnifiedTopology: true });\n\n// Middleware configurations\napp.use(helmet()); // Security enhancements\napp.use(cors()); // Enable CORS\napp.use(express.json()); // Parse JSON bodies\n\n// Rate limiter middleware\nconst limiter = rateLimit({\n windowMs: 1 * 60 * 1000, // 1 minute\n max: 100 // Limit each IP to 100 requests per windowMs\n});\napp.use(limiter);\n\n// Define Todo schema and model\nconst todoSchema = new mongoose.Schema({\n title: String,\n completed: Boolean\n});\nconst Todo = mongoose.model('Todo', todoSchema);\n\n// Define routes\napp.delete('/todo/:todoId', deleteTodoTask);\n\nasync function deleteTodoTask(req, res) {\n const { todoId } = req.params;\n try {\n if (!todoId) {\n return res.status(400).json({ success: false, message: 'Todo ID must be provided.' });\n }\n const deletedTodo = await Todo.findByIdAndDelete(todoId);\n if (!deletedTodo) {\n return res.status(404).json({ success: false, message: 'Todo not found.' });\n }\n return res.status(200).json({ success: true, message: 'Todo deleted successfully.' });\n } catch (error) {\n console.error('Error deleting todo:', error);\n return res.status(500).json({ success: false, message: 'An error occurred while deleting the todo.' });\n }\n}\n\n// Error handling middleware\napp.use((err, req, res, next) => {\n console.error(err.stack);\n res.status(500).send('Something broke!');\n});\n\n// Start the server\napp.listen(PORT, () => {\n console.log(`Server is running on http://localhost:${PORT}`);\n});" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.