Files
introduction-to-testing/examples/task-list/server/index.js
Steve Kinney 4e4978cb8a Squashed commit of the following:
commit bfeb85e018267cd709cb8a3f4a072ed1c1e8a0e7
Author: Steve Kinney <hello@stevekinney.net>
Date:   Wed Oct 2 04:34:37 2024 -0500

    Remove solution

commit 8c740cf91bf10043f5740716dffdf80f142f16d5
Author: Steve Kinney <hello@stevekinney.net>
Date:   Wed Oct 2 04:32:42 2024 -0500

    Complete MSW example

commit 0a646ca6e16460b94174dadf629cdd9866c9f33b
Author: Steve Kinney <hello@stevekinney.net>
Date:   Wed Oct 2 04:25:06 2024 -0500

    Minor tweaks to Task List
2024-10-02 04:34:56 -05:00

66 lines
1.4 KiB
JavaScript

import express from 'express';
import bodyParser from 'body-parser';
import chalk from 'chalk';
import {
getTasks,
getTask,
createTask,
updateTask,
deleteTask,
} from './tasks.js';
const app = express();
app.use(bodyParser.json());
app.get('/api/tasks', (req, res) => {
const tasks = getTasks();
res.json(tasks);
});
app.post('/api/tasks', (req, res) => {
const { title } = req.body;
if (!title) {
return res.status(400).json({ message: 'A title is required' });
}
const task = createTask(title);
res.status(201).json(task);
});
app.get('/api/tasks/:id', (req, res) => {
const task = getTask(req.params.id);
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.json(task);
});
app.patch('/api/tasks/:id', (req, res) => {
const { title, completed } = req.body;
const task = updateTask(req.params.id, { title, completed });
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.sendStatus(204);
});
app.delete('/api/tasks/:id', (req, res) => {
const task = deleteTask(req.params.id);
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.sendStatus(204); // No content to send back
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(chalk.magenta(`Server is running on port ${chalk.green(PORT)}`));
});