-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
195 lines (172 loc) · 5.78 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Arquivo principal do aplicativo.
* Configura e inicia o servidor Express.
* Define rotas para manipulação de tarefas.
* @module taskhub
* @author Luciano Édipo
* @license CC0-1.0
* @version 1.0.0
* @requires express, body-parser, path
* @see {@link http://www.ledipo.eti.br/|Luciano Édipo}
*/
const express = require('express');
const bodyParser = require('body-parser');
const packageInfo = require('./package.json'); // Adicionado para acessar informações do package.json
const path = require('path'); // Adicionado para manipulação de caminhos
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Servir arquivos estáticos da pasta 'public'
app.use(express.static(path.join(__dirname, 'public')));
/**
* Array de tarefas.
* Cada tarefa é um objeto com os campos id, title, description e completed.
* @type {Array}
*/
let tasks = [
{
id: "1",
title: "Fazer Compras",
description: "Comprar itens essenciais no mercado",
completed: false
},
{
id: "2",
title: "Estudar Node.js",
description: "Aprender sobre Node.js e construir aplicativos",
completed: true
},
{
id: "3",
title: "Fazer Exercícios",
description: "Realizar exercícios físicos diários",
completed: false
}
];
/**
* Rota para exibir a página inicial.
* @name GET /
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
/**
* Rota para exibir a página de tarefas.
* @name GET /tasks
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/tasks', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'tasks.html'));
});
/**
* Rota para obter todas as tarefas.
* @name GET /api/tasks
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/api/tasks', (req, res) => {
res.json(tasks);
});
/**
* Rota para adicionar uma nova tarefa.
* @name POST /api/tasks
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.post('/api/tasks', (req, res) => {
const newTask = req.body;
const newId = tasks.length + 1;
newTask.id = newId.toString();
newTask.completed = false;
tasks.push(newTask);
console.log('Nova tarefa adicionada:', newTask);
res.json({ message: 'Tarefa adicionada com sucesso!', task: newTask });
});
/**
* Rota para exibir a página de adicionar tarefas.
* @name GET /tasks/add
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/tasks/add', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'add-task.html'));
});
/**
* Rota para exibir os detalhes de uma tarefa específica.
* @name GET /task/:taskId
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/task/:taskId', (req, res) => {
// Recupere o ID da tarefa da solicitação
const taskId = req.params.taskId;
// Aqui você pode buscar os detalhes da tarefa com base no ID, por exemplo, em seu array `tasks`
// Supondo que `tasks` seja um array de objetos que tem um campo `id` correspondente ao taskId
const task = tasks.find(task => task.id === taskId);
if (!task) {
// Se a tarefa não for encontrada, retorne um status 404 (Not Found)
res.status(404).send('Tarefa não encontrada');
return;
}
// Renderize uma página (por exemplo, task-view.html) com os detalhes da tarefa
res.sendFile(path.join(__dirname, 'public', 'task-view.html'));
});
/**
* Rota para obter os detalhes de uma tarefa com base no ID.
* @name GET /api/task/:taskId
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.get('/api/task/:taskId', (req, res) => {
// Recupere o ID da tarefa da solicitação
const taskId = req.params.taskId;
// Aqui você pode buscar os detalhes da tarefa com base no ID, por exemplo, em seu array `tasks`
// Supondo que `tasks` seja um array de objetos que tem um campo `id` correspondente ao taskId
const task = tasks.find(task => task.id === taskId);
if (!task) {
// Se a tarefa não for encontrada, retorne um status 404 (Not Found)
res.status(404).json({ error: 'Tarefa não encontrada' });
return;
}
// Retorne os detalhes da tarefa em formato JSON
res.json(task);
});
/**
* Rota para marcar uma tarefa como concluída.
* @name PUT /api/task/:taskId/complete
* @function
* @param {Object} req - Objeto de requisição do Express.
* @param {Object} res - Objeto de resposta do Express.
*/
app.put('/api/task/:taskId/complete', (req, res) => {
const taskId = req.params.taskId;
const taskIndex = tasks.findIndex(task => task.id === taskId);
if (taskIndex !== -1) {
tasks[taskIndex].completed = true;
res.json({ message: 'Tarefa marcada como concluída!', task: tasks[taskIndex] });
} else {
res.status(404).json({ message: 'Tarefa não encontrada.' });
}
});
/**
* Inicia o servidor Express.
* @name listen
* @function
* @param {number} port - O número da porta em que o servidor será executado.
* @param {Function} callback - Função de retorno de chamada para ser executada quando o servidor estiver pronto para aceitar conexões.
*/
app.listen(port, () => {
console.log(`Servidor rodando em http://localhost:${port}`);
console.log(`Versão do ${packageInfo.name}: ${packageInfo.version}`); // Adicionado para exibir informações do pacote
});