使用node.js将表单数据发布到mysql

n53p2ov0  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(394)

我试图在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)

索引.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>

索引.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数据库
我的数据库应该是这样的

0g0grzrc

0g0grzrc1#

我已经按照指定的问题重构了你的代码。例如,你需要提及公共目录中的所有静态内容(即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 }); 由@pogrindis指定

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');
});

请按照以下文件路径操作:

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

相关问题