|
|
|
|
|
//16.apiRouter.js
|
|
|
|
|
|
const express = require('express')
|
|
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
|
|
|
|
|
|
|
const router = express.Router()
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
|
|
|
|
const mysql = require('mysql2');
|
|
|
|
|
|
const moment = require("moment");
|
|
|
|
|
|
|
|
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pool = mysql.createPool({
|
|
|
|
|
|
host: 'localhost',
|
|
|
|
|
|
user: 'root',
|
|
|
|
|
|
password: 'Skyinno251,',
|
|
|
|
|
|
database: 'appserver',
|
|
|
|
|
|
multipleStatements: true, // 允许执行多条语句,(高并发方式)
|
|
|
|
|
|
connectionLimit: 10 // 连接池最大连接数
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在这里挂载对应的路由
|
|
|
|
|
|
router.get('/get', (req, res) => {
|
|
|
|
|
|
// 通过 req.query 获取客户端发送到服务器的数据(查询字符串)
|
|
|
|
|
|
const query = req.query
|
|
|
|
|
|
|
|
|
|
|
|
run().then(r => {console.log("调用oracle")})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 调用 res.send() 方法,向客户端响应处理的结果
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 0,// 0 表示处理成功,1 表示处理失败
|
|
|
|
|
|
msg: 'GRT请求成功', // 状态的描述
|
|
|
|
|
|
data: query// 需要响应给客户端的数据
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/addTimeNow', (req, res) => {
|
|
|
|
|
|
// 通过 req.body 获取请求体中包含的 url-encoded 格式的数据
|
|
|
|
|
|
const body = req.body
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 1,
|
|
|
|
|
|
msg: 'POST请求成功',
|
|
|
|
|
|
data: body
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 新增用户 POST 请求处理程序
|
|
|
|
|
|
router.post('/addUserNow', (req, res) => {
|
|
|
|
|
|
const date = new Date();
|
|
|
|
|
|
const formattedDate = moment(date).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
console.log(formattedDate);
|
|
|
|
|
|
|
|
|
|
|
|
const { name, age } = req.body; // 从请求体中获取数据
|
|
|
|
|
|
|
|
|
|
|
|
if (name == undefined||name == ''||name == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'name not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (age == undefined||age == ''||age == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'age not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const values = [name, age, date];
|
|
|
|
|
|
const insertSql = `INSERT INTO user_now (name, age,create_time) VALUES (?, ?, ?)`;
|
|
|
|
|
|
pool.query(insertSql, values, (err, results) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
code:'200',
|
|
|
|
|
|
data:results,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.delete('/delete', (req, res) => {
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 0,
|
|
|
|
|
|
msg: 'DELETE请求成功',
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 获取用户详情 3
|
|
|
|
|
|
router.get('/user/:username', (req, res) => {
|
|
|
|
|
|
const { username } = req.params;
|
|
|
|
|
|
if (username == undefined||username == ''||username == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'username not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
const values =[username];
|
|
|
|
|
|
let query = 'SELECT * FROM user WHERE username = ?';
|
|
|
|
|
|
pool.query(query,values,(err, results) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
code:'200',
|
|
|
|
|
|
data:results?results[0]:{},
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/user', (req, res) => {
|
|
|
|
|
|
const { username, name } = req.query;
|
|
|
|
|
|
//const values =[username, name];
|
|
|
|
|
|
let query = 'SELECT * FROM user';
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
|
const params = [];
|
|
|
|
|
|
if (name !== undefined&&name !== ''&&name !== null) {
|
|
|
|
|
|
query += ' WHERE name = ?';
|
|
|
|
|
|
params.push(name);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (username !== undefined&&username !== ''&&username !== null) {
|
|
|
|
|
|
query += params.length ? ' AND' : ' WHERE';
|
|
|
|
|
|
query += ' username = ?';
|
|
|
|
|
|
params.push(username);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询数据库并返回数据
|
|
|
|
|
|
pool.query(query,params,(err, results) => {
|
|
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
code:'200',
|
|
|
|
|
|
data:results,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.put('/userPut', (req, res) => {
|
|
|
|
|
|
// console.log(req.body);
|
|
|
|
|
|
const { name, age ,id} = req.body; // 从请求体中获取数据
|
|
|
|
|
|
|
|
|
|
|
|
if (id == undefined||id == ''||id == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'id not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (name == undefined||name == ''||name == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'name not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (age == undefined||age == ''||age == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'age not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const values = [name, age, id];
|
|
|
|
|
|
// 准备 SQL 插入语句
|
|
|
|
|
|
const sql = "UPDATE user_now SET name = ?, age = ? WHERE id = ?";
|
|
|
|
|
|
pool.query(sql, values, (err, results) => {
|
|
|
|
|
|
// console.log(err,'err');
|
|
|
|
|
|
// console.log(results,'results');
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
code:'200',
|
|
|
|
|
|
data:results,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 删除用户数据 DELETE请求处理程序
|
|
|
|
|
|
router.delete('/userDel/:id', (req, res) => {
|
|
|
|
|
|
// const {id} = req.body; // 从请求体中获取数据
|
|
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
|
|
|
|
if (id == undefined||id == ''||id == null) {
|
|
|
|
|
|
res.status(500).json({ error: 'id not null' });
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(id,'id');
|
|
|
|
|
|
const values = [id];
|
|
|
|
|
|
const sql = "DELETE FROM time_now WHERE id = ?";
|
|
|
|
|
|
pool.query(sql, values, (err, results) => {
|
|
|
|
|
|
// console.log(err,'err');
|
|
|
|
|
|
// console.log(results,'results');
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
code:'200',
|
|
|
|
|
|
data:results,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/process_get', function (req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
// 输出 JSON 格式
|
|
|
|
|
|
var response = {
|
|
|
|
|
|
"first_name":req.query.first_name,
|
|
|
|
|
|
"last_name":req.query.last_name
|
|
|
|
|
|
};
|
|
|
|
|
|
console.log(response);
|
|
|
|
|
|
res.end(JSON.stringify(response));
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 application/x-www-form-urlencoded 编码解析
|
|
|
|
|
|
const urlencodedParser = bodyParser.urlencoded({ extended: false })
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/process_post', urlencodedParser, function (req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
// 输出 JSON 格式
|
|
|
|
|
|
var response = {
|
|
|
|
|
|
"first_name":req.body.first_name,
|
|
|
|
|
|
"last_name":req.body.last_name
|
|
|
|
|
|
};
|
|
|
|
|
|
console.log(response);
|
|
|
|
|
|
res.end(JSON.stringify(response));
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/file_upload', function (req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
console.log(req.files[0]); // 上传的文件信息
|
|
|
|
|
|
|
|
|
|
|
|
const des_file = "/Users/admin/tmp/" + req.files[0].originalname;
|
|
|
|
|
|
fs.readFile( req.files[0].path, function (err, data) {
|
|
|
|
|
|
fs.writeFile(des_file, data, function (err) {
|
|
|
|
|
|
if( err ){
|
|
|
|
|
|
console.log( err );
|
|
|
|
|
|
}else{
|
|
|
|
|
|
response = {
|
|
|
|
|
|
message:'File uploaded successfully',
|
|
|
|
|
|
filename:req.files[0].originalname
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log( response );
|
|
|
|
|
|
res.end( JSON.stringify( response ) );
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/getCookies', function(req, res) {
|
|
|
|
|
|
console.log("Cookies: " + util.inspect(req.cookies));
|
|
|
|
|
|
res.end( JSON.stringify( util.inspect(req.cookies) ) );
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router
|