我正在尝试使此全栈Web应用程序正常工作。每当我运行服务器时,它都会说它已连接,但是当我尝试在浏览器(Google Chrome)上加载该网站时,它将加载几分钟,然后我在屏幕上收到此消息,提示“该页面无法正常工作本地主机未发送任何数据“ ERR_EMPTY_RESPONSE”。这很奇怪,因为我没有在前端或后端的终端上收到任何错误。
这是我的server.js文件:
const express = require('express');
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const exphbs = require('express-handlebars');
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
const routes = require('./controllers/wh40k_controllers');
app.use(routes);
app.listen(PORT, function() {
console.log("Server listening on: http://localhost:" + PORT);
});
这是我的控制器文件:
const express = require('express');
const wh40k = require('../models/wh40k');
const router = express.Router();
router.get("/", function(req, res) {
wh40k.selectAll(function(data) {
let hbsObject = {
wh40k: data
};
console.log(hbsObject);
res.render("index", hbsObject);
});
});
router.post("/api/wh40k", function(req, res) {
wh40k.insertOne(["figure_name", "faction", "role", "own"],
[req.body.figureName, req.body.faction, req.body.role, req.body.own],
function(data) {
res.json({ figure_name: data.insertName });
});
});
router.put("/api/wh40k/:name", function(req, res) {
let condition = "name = " + req.params.figure_name;
wh40k.updateOne(
{
figure_name: req.body.figure_name,
faction: req.body.faction,
role: req.body.role,
own: req.body.own
},
condition,
function(result) {
if(result === 0) {
return res.status(404).end();
}
res.status(200).end();
}
);
});
module.exports = router;
这是我的连接文件:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: "localhost",
port: 8889,
user: "root",
password: "root",
database: "wh40k_db"
});
function handleDisconnect() {
connection = mysql.createConnection(db_config);
}
connection.connect(function(err) {
if (err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
module.exports = connection;
这是我的模型文件:
const orm = require('../config/orm.js');
let wh40k = {
selectAll: function(cb) {
orm.selectAll("figures", function(res) {
cb(res);
});
},
insertOne: function(cols, vals, cb) {
orm.insertOne("figures", cols, vals, function(res) {
cb(res);
});
},
updateOne: function(obj, condition, cb) {
orm.updateOne("figures", obj, condition, function(res) {
cb(res);
});
}
};
module.exports = wh40k;
这是我的Orm文件:
const connection = require('../config/connection.js');
let orm = {
selectAll: function(tableSelect) {
connection.query("SELECT * FROM ??", [tableSelect], function(err, result) {
if (err) throw err;
console.log(result);
});
},
insertOne: function(newInfo, tableSelect) {
connection.query("INSERT ?? INTO ??", [newInfo, tableSelect], function(err, result) {
if (err) throw err;
console.log(result);
});
},
updateOne: function(updatedInfo, tableSelect) {
connection.query("UPDATE ?? WHERE ??", [updatedInfo, tableSelect], function(err, result) {
if (err) throw err;
console.log(result);
});
}
};
module.exports = orm;
我希望该页面实际上使用MySQL的网页以把手的形式从MySQL数据库中加载信息,但是,如前所述,它不会这样做,而是表示该页面无法加载。