var fs = require('fs'); var ejs = require('ejs'); var mysql = require('mysql'); var express = require('express'); var bodyParser = require('body-parser'); // 连接mysql数据库 var client = mysql.createConnection({ host: 'localhost', port:"3306", user: 'root', password: 'root123', database: 'my_db_1' }); // //判断是否连接成功 client.connect(function(err){ if(err){ console.log('[query] - :'+err); return; } console.log('[connection connect] Mysql数据库连接成功!'); }); // 解析请求体中的URL的编码数据 var app = express(); app.use(bodyParser.urlencoded({ extended: false })); // app.listen(52273, function () { console.log('服务器运行在 http://127.0.0.1:52273'); }); // app.get('/', function (request, response) { // fs.readFile('book-list.html', 'utf8', function (error, data) { // client.query('SELECT * FROM books', function (error, results) { // response.send(ejs.render(data, { data: results })); }); }); }); // app.get('/delete/:id', function (request, response) { // client.query('DELETE FROM books WHERE id=?', [request.params.id], function () { // response.redirect('/'); }); }); // app.get('/insert', function (request, response) { // fs.readFile('book-insert.html', 'utf8', function (error, data) { // response.send(data); }); }); // app.post('/insert', function (request, response) { // var body = request.body; // client.query('INSERT INTO books (bookname, author, press) VALUES (?, ?, ?)', [ body.bookname, body.author, body.press ], function () { // response.redirect('/'); }); }); // app.get('/edit/:id', function (request, response) { // fs.readFile('book-edit.html', 'utf8', function (error, data) { // client.query('SELECT * FROM books WHERE id = ?', [ request.params.id ], function (error, result) { // response.send(ejs.render(data, { data: result[0] })); }); }); }); // app.post('/edit/:id', function (request, response) { // var body = request.body; // client.query('UPDATE books SET bookname=?, author=?, press=? WHERE id=?', [body.bookname, body.author, body.press, request.params.id], function () { // response.redirect('/'); }); });补全注释
时间: 2024-03-07 20:48:57 浏览: 149
Nodejs+express+ejs简单使用实例代码
这段代码是一个使用Node.js搭建的服务器,主要功能是连接MySQL数据库,并通过Express框架提供HTTP服务。以下是代码注释的补全:
```javascript
var fs = require('fs');
var ejs = require('ejs');
var mysql = require('mysql');
var express = require('express');
var bodyParser = require('body-parser');
// 连接mysql数据库
var client = mysql.createConnection({
host: 'localhost',
port:"3306",
user: 'root',
password: 'root123',
database: 'my_db_1'
});
// 判断是否连接成功
client.connect(function(err){
if(err){
console.log('[query] - :' + err);
return;
}
console.log('[connection connect] Mysql数据库连接成功!');
});
// 解析请求体中的URL的编码数据
var app = express();
app.use(bodyParser.urlencoded({
extended: false
}));
// 启动服务器
app.listen(52273, function () {
console.log('服务器运行在 http://127.0.0.1:52273');
});
// 处理根路径的GET请求
app.get('/', function (request, response) {
fs.readFile('book-list.html', 'utf8', function (error, data) {
// 查询数据库并将结果渲染到模板中
client.query('SELECT * FROM books', function (error, results) {
response.send(ejs.render(data, {
data: results
}));
});
});
});
// 处理删除图书的GET请求
app.get('/delete/:id', function (request, response) {
client.query('DELETE FROM books WHERE id=?', [request.params.id], function () {
response.redirect('/');
});
});
// 处理插入图书的GET请求
app.get('/insert', function (request, response) {
fs.readFile('book-insert.html', 'utf8', function (error, data) {
response.send(data);
});
});
// 处理插入图书的POST请求
app.post('/insert', function (request, response) {
var body = request.body;
client.query('INSERT INTO books (bookname, author, press) VALUES (?, ?, ?)', [
body.bookname, body.author, body.press
], function () {
response.redirect('/');
});
});
// 处理编辑图书的GET请求
app.get('/edit/:id', function (request, response) {
fs.readFile('book-edit.html', 'utf8', function (error, data) {
client.query('SELECT * FROM books WHERE id = ?', [
request.params.id
], function (error, result) {
response.send(ejs.render(data, {
data: result[0]
}));
});
});
});
// 处理编辑图书的POST请求
app.post('/edit/:id', function (request, response) {
var body = request.body;
client.query('UPDATE books SET bookname=?, author=?, press=? WHERE id=?', [
body.bookname, body.author, body.press, request.params.id
], function () {
response.redirect('/');
});
});
```
阅读全文