почему при нажатии на кнопку удалить, показывает ошибку Cannot GET /abonent/2/remove
const express = require('express'),
mysql2 = require('mysql2/promise'),
bodyParser = require('body-parser'),
fs = require('fs'),
nodePath = require('path');
const pool = mysql2.createPool({
host: 'localhost',
user: 'root',
database: 'task2',
password: '',
});
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/styles.css', (req, res) => {
res.sendFile(__dirname + '/styles.css');
});
const nav = ({ path }) => `<ul>
<li>${path == '/' ? '<span>Cписок студентов</span>' : '<a href="/">Cписок студентов</a>'}</li>
</ul>`;
function loadPages(pages) {
pages.forEach(it => {
const [_, method] = nodePath.parse(it).name.split('@');
const [path, action] = require('./pages/' + it);
app[method](path, async (req, res) => {
const result = await action(pool, req);
if (typeof result == 'function') {
result(res);
} else {
res.send('<!DOCTYPE html><html><head><link rel="stylesheet" href="/styles.css"/></head><body><nav>' + nav(req) + '</nav><main>' + result + '</main></body></html>');
}
});
});
}
const listOfPages = fs.readdirSync('./pages');
loadPages(listOfPages);
app.listen(3000, function() {
console.log('server started at', this.address().port);
});
* {
box-sizing: border-box;
}
nav, main {
width: 1024px;
margin: 0 auto;
}
main {
background-color: whitesmoke;
padding: 10px;
}
nav > ul {
list-style: none;
display: flex;
padding: 0;
gap: 5px;
}
nav > ul > li > a, nav > ul > li > span {
display: block;
text-decoration: none;
padding: 5px;
}
nav > ul > li > a {
background-color: gray;
color: white;
}
nav > ul > li > span {
background-color: whitesmoke;
color: black;
}
h1 {
padding: 0;
margin-top: 0;
margin-bottom: 5px;
display: flex;
align-items: center;
}
h1 > form {
margin-left: 7px;
display: flex;
}
h1 > button {
margin-left: 7px;
}
button > a {
text-decoration: none;
color: black;
}
.error {
color: red;
}
module.exports = ['/add-abonent', async function (pool, { query, body }) {
return `
<h1>Добавление студента</h1>
<form method="post" action="/add-abonent">
<input type="text" placeholder="Имя студента" minLength="2" name="name"/>
<input type="text" placeholder="Фамилия студента" minLength="2" name="lastname"/>
<input type="text" placeholder="Отчество студента" minLength="2" name="dadname"/>
<input type="text" placeholder="Группа студента" minLength="2" name="clas"/>
<input type="text" placeholder="Дата рождения" name="datee"/>
<input type="submit" value="Добавить"/>
</form>
`;
}];
module.exports = ['/add-abonent', async function (pool, { body }) {
const { name, lastname, dadname, clas, datee} = body;
await pool.query('INSERT INTO abonents SET ?', {
name, lastname, dadname, clas, datee,
});
return res => res.redirect('/');
}];
module.exports = ['/', async function (pool, { params, body }) {
const [abonents] = await pool.query('SELECT * FROM abonents');
return `
<h1>Список студентов</h1>
<ul>
${abonents.map(abonent => `<li><p="${abonent.id}">${abonent.name} - ${abonent.lastname} - ${abonent.dadname} - ${abonent.clas}
<a href="/abonent/${abonent.id}/remove">Удалить</a></p></li>`).join('')}
</ul>
<button><a href="/add-abonent">Добавить студента</a></button>
`;
}];
module.exports = ["/abonent/:abonent_id/remove", async function (pool, { params }) {
const { abonent_id } = params;
await pool.query('DELETE FROM abonents WHERE id = ?', abonent_id);
}];