【发布时间】:2019-04-08 02:40:45
【问题描述】:
我越来越糊涂了。我在 mySql 中看到的所有教程都以这样的方式结束:
在模型/dbconnection.js 中
var mysql = require('mysql');
port = process.env.PORT || 3333;
if (port == 3333) {
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'root',
database: 'nameDataBase',
insecureAuth: true
});
} else {
console.log("Error");
}
connection.connect();
module.exports = connection;
然后在 routes/user.js 中
...
router.delete("/:id", verifyToken, (req, res) => {
const newLocal = "DELETE FROM login_user WHERE id = ?";
connection.query(newLocal, [req.params.id], (err,rows,fields) => {
if (err) {
res.sendStatus(500);
return;
}
console.log(rows.affectedRows);
res.status(200).send({delete: rows});
});
});
module.exports = router;
模型和控制器没有在这里混在一起吗?如果明天我想更改数据库的类型,我必须在模型和路由中进行更改。我不应该在 models/user.js 中创建诸如 getAllUsersBlaBla(params) 之类的函数,然后从 routes/user.js 调用它吗?
【问题讨论】: