node.js MySQL 学生管理系统
时间: 2023-12-14 13:34:29 浏览: 108
mysql 学生管理系统
5星 · 资源好评率100%
以下是使用Node.js和MySQL构建学生管理系统的基本步骤:
1.安装Node.js和MySQL,并创建一个新的数据库。
2.使用npm安装所需的依赖项,例如express、mysql和body-parser。
3.创建一个名为server.js的新文件,并在其中设置服务器。
4.创建一个名为student.js的新文件,并在其中定义学生模型。
5.创建一个名为studentController.js的新文件,并在其中定义处理学生请求的控制器。
6.创建一个名为studentRoute.js的新文件,并在其中定义处理学生路由的路由器。
7.将studentRoute.js添加到server.js中。
8.创建一个名为index.html的新文件,并在其中定义学生管理系统的前端。
9.将index.html添加到studentRoute.js中。
10.启动服务器并在浏览器中打开学生管理系统。
下面是一个简单的示例代码,用于演示如何使用Node.js和MySQL构建学生管理系统:
```javascript
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const student = require('./studentRoute');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'students'
});
connection.connect();
app.use('/students', student);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
```javascript
// student.js
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'students'
});
connection.connect();
const Student = function(student) {
this.name = student.name;
this.email = student.email;
this.phone = student.phone;
};
Student.create = (newStudent, result) => {
connection.query('INSERT INTO students SET ?', newStudent, (err, res) => {
if (err) {
console.log('Error:', err);
result(err, null);
} else {
console.log(res.insertId);
result(null, res.insertId);
}
});
};
Student.findById = (id, result) => {
connection.query('SELECT * FROM students WHERE id = ?', id, (err, res) => {
if (err) {
console.log('Error:', err);
result(err, null);
} else {
result(null, res);
}
});
};
Student.findAll = result => {
connection.query('SELECT * FROM students', (err, res) => {
if (err) {
console.log('Error:', err);
result(err, null);
} else {
result(null, res);
}
});
};
Student.updateById = (id, student, result) => {
connection.query(
'UPDATE students SET name = ?, email = ?, phone = ? WHERE id = ?',
[student.name, student.email, student.phone, id],
(err, res) => {
if (err) {
console.log('Error:', err);
result(err, null);
} else {
result(null, res);
}
}
);
};
Student.deleteById = (id, result) => {
connection.query('DELETE FROM students WHERE id = ?', id, (err, res) => {
if (err) {
console.log('Error:', err);
result(err, null);
} else {
result(null, res);
}
});
};
module.exports = Student;
```
```javascript
// studentController.js
const Student = require('./student');
exports.create = (req, res) => {
const newStudent = new Student(req.body);
if (!newStudent.name || !newStudent.email || !newStudent.phone) {
res.status(400).send({ error: true, message: 'Please provide all fields' });
} else {
Student.create(newStudent, (err, student) => {
if (err) {
res.send(err);
} else {
res.json({ error: false, message: 'Student added successfully!', data: student });
}
});
}
};
exports.findById = (req, res) => {
Student.findById(req.params.id, (err, student) => {
if (err) {
res.send(err);
} else {
res.json(student);
}
});
};
exports.findAll = (req, res) => {
Student.findAll((err, student) => {
if (err) {
res.send(err);
} else {
res.json(student);
}
});
};
exports.updateById = (req, res) => {
const student = new Student(req.body);
if (!student.name || !student.email || !student.phone) {
res.status(400).send({ error: true, message: 'Please provide all fields' });
} else {
Student.updateById(req.params.id, student, (err, student) => {
if (err) {
res.send(err);
} else {
res.json({ error: false, message: 'Student updated successfully!', data: student });
}
});
}
};
exports.deleteById = (req, res) => {
Student.deleteById(req.params.id, (err, student) => {
if (err) {
res.send(err);
} else {
res.json({ error: false, message: 'Student deleted successfully!', data: student });
}
});
};
```
```javascript
// studentRoute.js
const express = require('express');
const router = express.Router();
const studentController = require('./studentController');
router.post('/', studentController.create);
router.get('/', studentController.findAll);
router.get('/:id', studentController.findById);
router.put('/:id', studentController.updateById);
router.delete('/:id', studentController.deleteById);
module.exports = router;
```
阅读全文