a simple to-do list app built with Node.js and Express. Users can add and delete tasks, and the tasks are saved in a JSON file. This is a basic example that can be expanded with more features.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Todo App</title> <style> body { font-family: Arial, sans-serif; } .todo { margin: 5px 0; } </style> </head> <body> <h1>Todo List</h1> <input id="taskInput" type="text" placeholder="New task"> <button onclick="addTodo()">Add Todo</button> <div id="todoList"></div> <script> async function fetchTodos() { const response = await fetch('/api/todos'); const todos = await response.json(); const todoList = document.getElementById('todoList'); todoList.innerHTML = ''; todos.forEach(todo => { const todoDiv = document.createElement('div'); todoDiv.classList.add('todo'); todoDiv.innerHTML = `${todo.task} <button onclick="deleteTodo(${todo.id})">Delete</button>`; todoList.appendChild(todoDiv); }); } async function addTodo() { const taskInput = document.getElementById('taskInput'); const task = taskInput.value; if (!task) return; await fetch('/api/todos', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ task }), }); taskInput.value = ''; fetchTodos(); } async function deleteTodo(id) { await fetch(`/api/todos/${id}`, { method: 'DELETE', }); fetchTodos(); } fetchTodos(); </script> </body> </html>
body { font-family: Arial, sans-serif; background-color: #f4f4f9; color: #333; margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center; } h1 { color: #4a4a4a; } #taskInput { width: 300px; padding: 10px; border: 2px solid #4a90e2; border-radius: 4px; margin-right: 10px; outline: none; transition: border-color 0.3s; } #taskInput:focus { border-color: #007bff; } button { padding: 10px 15px; background-color: #4a90e2; border: none; border-radius: 4px; color: white; cursor: pointer; transition: background-color 0.3s; } button:hover { background-color: #007bff; } #todoList { margin-top: 20px; width: 100%; max-width: 400px; } .todo { background: white; padding: 10px; margin: 5px 0; border-radius: 4px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); display: flex; justify-content: space-between; align-items: center; } .todo button { background-color: #e74c3c; padding: 5px 10px; border: none; border-radius: 4px; color: white; cursor: pointer; transition: background-color 0.3s; } .todo button:hover { background-color: #c0392b; }
const express = require('express'); const bodyParser = require('body-parser'); const fs = require('fs'); const path = require('path'); const app = express(); const PORT = 3000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); const dataFilePath = path.join(__dirname, 'todos.json'); // Load existing todos or initialize an empty array let todos = []; if (fs.existsSync(dataFilePath)) { const data = fs.readFileSync(dataFilePath); todos = JSON.parse(data); } // Get all todos app.get('/api/todos', (req, res) => { res.json(todos); }); // Add a new todo app.post('/api/todos', (req, res) => { const newTodo = { id: Date.now(), task: req.body.task, }; todos.push(newTodo); fs.writeFileSync(dataFilePath, JSON.stringify(todos)); res.status(201).json(newTodo); }); // Delete a todo by ID app.delete('/api/todos/:id', (req, res) => { todos = todos.filter(todo => todo.id !== parseInt(req.params.id)); fs.writeFileSync(dataFilePath, JSON.stringify(todos)); res.status(204).send(); }); // Start the server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });