gpt4 book ai didi

javascript - 使用 Node.js 将表单数据发布到 MySQL

转载 作者:行者123 更新时间:2023-11-30 21:44:20 27 4
gpt4 key购买 nike

我正在尝试在 Node 中编写一个简单的 server.js,它将我的 html 文件中的表单数据发布到 MySQL 中。但是我收到语法错误。我已经在下面发布了代码和错误。我正在努力解决这个问题。

错误

http://localhost:3000/submit

Error: No default engine was specified and no extension was provided.
at new View (C:\website\node_modules\express\lib\view.js:61:11)
at Function.render (C:\website\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\website\node_modules\express\lib\response.js:1008:7)
at C:\website\index.js:21:9
at Layer.handle [as handle_request] (C:\website\node_modules\express\lib\router\layer.js:95:5)
at next (C:\website\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\website\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\website\node_modules\express\lib\router\layer.js:95:5)
at C:\website\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\website\node_modules\express\lib\router\index.js:335:12)

index.html

<form action="/submit" method="post">
<input id="name" type="text" name="name" placeholder="Type your name...">
<input id="message" type="text" name="message" placeholder="Type message...">
<input class="submit_message" type="submit" value="Send">
</form>

index.js

var express = require('express');
var app = express();
var mysql = require('mysql');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('public'));

var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mywebsite"
});

connection.connect();

app.get('/submit',function(req,res) {
res.render('index');
});

app.post('/submit',urlencodedParser, function(req, res, next) {
console.log(req.body.name);
console.log(req.body.message);
connection.connect(function(err) {
if (err) throw err;
console.log("connected");
var sql = "INSERT INTO `users` (`name`,`message`) VALUES ('" + req.body.name + "', '" + req.body.message + "')";
con.query(sql, function(err, result) {
if(err) throw err;
console.log("table created");
});
});
res.render('index', {title: 'Express'});
});

connection.end();

app.listen(3000, function () {
console.log('Listening on port 3000');
});

MySQL 数据库

我的数据库应该是这样的

enter image description here

最佳答案

我已经根据指定的问题重构了您的代码。例如-您需要在公共(public)目录中提及我们所有的静态内容(即您的 index.html),您还犯了语法错误,即-您编写了 con.query(sql, function(err, result) 而不是 connection.query(sql, function (err, result),而不是像你那样渲染 index.html 文件 res.render('index', {title: 'Express '}); 最好将文件发送到 public/index.html res.sendFile('public/index.html', { root: __dirname }); @波格林迪斯


var express = require('express');
var app = express();
var mysql = require('mysql');

var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mywebsite"
});


var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));

app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {
res.render('index.html');
});

app.post('/submit', urlencodedParser, function (req, res) {
console.log("Im here");
console.log(req.body.name);
console.log(req.body.message);
connection.connect(function (err) {
if (err) throw err;
console.log("connected");
var sql = "INSERT INTO `users` (`name`,`message`) VALUES ('" + req.body.name + "', '" + req.body.message + "')";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("table created");
});
});
res.sendFile('public/index.html', { root: __dirname });
});


app.listen(3000, function () {
console.log('Listening on port 3000');
});

请按照文件路径为: enter image description here

我成功地将数据保存到我的数据库中,这些数据是从 index.html 中的输入文本框(名称、消息)输入的。编码愉快!!

关于javascript - 使用 Node.js 将表单数据发布到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50118028/

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