gpt4 book ai didi

mysql - 无法 POST/api/dogs

转载 作者:行者123 更新时间:2023-11-29 15:34:59 26 4
gpt4 key购买 nike

我在nodejs中创建了一个基本项目,但是当我提交数据时,它说无法发布api/dogs。 https://vfs.cloud9.us-east-2.amazonaws.com/vfs/3199fe30e54d4e63aa04c42b20416f0e/preview/index.html用于现场测试
这是我的index.html

            <h1>Dogs</h1>
<h2>Add a Dog</h2>
<form action="/api/dogs" method="POST">
<label for="name">Enter Dog Name: </label>
<input type="text" id="name" name="name" autofocus />
<br/>
<label for="description">Enter Dog Description </label>
<textarea type="description" id="description" name="description" >
</textarea>
<input type="submit" value="Submit" />

创建一个名为 data 的新文件夹,并在该文件夹中创建一个名为 connection.js 的文件。该文件将导出一个数据库连接池。

exports.connectionPool = mysql.createPool({
connectionLimit:10,
host : 'localhost',
user : 'root',
password : 'root',
database : 'a1',
multipleStatements: true
});

在数据中创建另一个名为 setup.js 的文件。我们将使用此文件在数据库中设置所需的表。事实上,我们的数据库将只包含一张表。

const {
connectionPool
} = require('./connection.js'); // requires a file in current directory

connectionPool.query('DROP TABLE IF EXISTS dogs;', (error, results) => {
if (error) {
console.log(error.message);
process.exit();
} else {
console.log("Success: dog table dropped!");
}
});

var createSql = "CREATE TABLE dogs (dog_id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, PRIMARY KEY (dog_id) );";
connectionPool.query(createSql, (error, results) => {
if (error) {
console.log(error.message);
process.exit();
} else {
console.log("Success: dog table Created!");
}
});

这是index.js代码

const {
connectionPool
} = require('./data/connection.js');

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

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({
extended: true
})

app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + "/" + "index.html");
})

app.post('/api/dogs', urlencodedParser, function (req, res) {
// Prepare output in JSON format

connectionPool.query("INSERT INTO dogs (name, description) VALUES ('" + req.body.name + "', '" + req.body.description + "');", (error, results) => {
if (error) {
console.log(error.message);
process.exit();
} else {
console.log("Success: dog inserted");
}
});
res.end(JSON.stringify(req.body));
})

app.get('/api/dogs', function (req, res) {
connectionPool.query("SELECT * FROM dogs LIMIT " + req.query.offset + ", " + req.query.count, (error, results) => {
if (error) {
console.log(error.message);
process.exit();
} else {
if (results === undefined || results.length == 0) {
res.status(404).send("nothing found");
} else {
console.log(results);
res.end(JSON.stringify(results));
}
}
});
})

app.get('/api/dogs/id', function (req, res) {
connectionPool.query("SELECT * FROM dogs where dog_id='" + req.query.id + "'", (error, results) => {
if (error) {
console.log(error.message);
process.exit();
} else {
console.log(results);
res.end(JSON.stringify(results));
}
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)
})


[1]: https://vfs.cloud9.us-east-2.amazonaws.com/vfs/3199fe30e54d4e63aa04c42b20416f0e/preview/index.html

最佳答案

您的表单操作不完整,您必须调用绝对网址来击中您的后弯

<form action="http://localhost:8081/api/dogs" method="POST">

关于mysql - 无法 POST/api/dogs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335224/

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