我正在尝试在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数据库
这就是我的数据库应该是这样的
答案 0 :(得分:0)
我已根据指定的问题重构了您的代码。对于ex-你需要提到公共目录中的所有静态内容(即你的index.html),你也做了语法错误,即你写了con.query(sql, function(err, result)
而不是connection.query(sql, function (err, result)
,而不是渲染索引.html文件就像你res.render('index', {title: 'Express'});
一样,你最好将文件发送到@Pogrindis指定的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');
});
我已成功将数据保存到我的数据库中,该数据库已从index.html的输入文本框(名称,消息)输入。 快乐编码!!