gpt4 book ai didi

mysql - express.js 中的 API 删除方法错误

转载 作者:行者123 更新时间:2023-11-29 10:08:42 25 4
gpt4 key购买 nike

当 id 在 App.delete 方法中不可用时如何处理删除查询不会给出错误。在两种情况下都显示成功。如果 id 不可用,那么它应该输出 id not available to delete task 。通过 id 方法获取的情况相同。如果 id 可用,则工作正常。如果 id 不可用,则不会显示错误

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mysql = require('mysql');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));

// connection configurations
const mc = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: '1234'
});

// connect to database
mc.connect();

// default route
app.get('/', function (req, res) {
return res.send({ error: true, message: 'hello' })
});

// Retrieve all todos
app.get('/todos', function (req, res) {
mc.query('SELECT * FROM tasks', function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results, message: 'Todos list.' });
});
});

// Search for todos with ‘bug’ in their name
app.get('/todos/search/:keyword', function (req, res) {
let keyword = req.params.keyword;
mc.query("SELECT * FROM tasks WHERE task LIKE ? ", ['%' + keyword + '%'], function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results, message: 'Todos search list.' });
});
});

// Retrieve todo with id
app.get('/todo/:id', function (req, res) {

let task_id = req.params.id;

mc.query('SELECT * FROM tasks where id=?', task_id, function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results[0], message: 'Todos list.' });
});

});

// Add a new todo
app.post('/todo', function (req, res) {

let task = req.body.task;

if (!task) {
return res.status(400).send({ error:true, message: 'Please provide task' });
}

mc.query("INSERT INTO tasks SET ? ", { task: task }, function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results, message: 'New task has been created successfully.' });
});
});

// Update todo with id
app.put('/todo', function (req, res) {

let task_id = req.body.task_id;
let task = req.body.task;

if (!task_id || !task) {
return res.status(400).send({ error: task, message: 'Please provide task and task_id' });
}

mc.query("UPDATE tasks SET task = ? WHERE id = ?", [task, task_id], function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results, message: 'Task has been updated successfully.' });
});
});

// Delete todo
app.delete('/todo/:id', function (req, res) {

let task_id = req.params.id;

if (!task_id) {
return res.status(400).send({ error: true, message: 'Please provide text_id' });
}
mc.query('DELETE FROM tasks WHERE id = ?', task_id, function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results, message: 'text has been Deleted successfully.' });
});
});

// all other requests redirect to 404
app.all("*", function (req, res) {
return res.status(404).send('page not found')
});

// port must be set to 8080 because incoming http requests are routed from port 80 to port 8080
app.listen(8080, function () {
console.log('Node app is running on port 8080');
});

// allows "grunt dev" to create a development server with livereload
//module.exports = app;

最佳答案

您必须将参数定义为可选。

Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern matching.

https://expressjs.com/en/guide/routing.html

适用于/todo 和/todo/{id},

路线 -/todo/:id*?

 //  Delete todo
app.delete('/todo/:id*?', function (req, res) {


});

关于mysql - express.js 中的 API 删除方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com