|
|
|
|
|
//16.apiRouter.js
|
|
|
|
|
|
const express = require('express')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const router = express.Router()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const oracledb = require('oracledb');
|
|
|
|
|
|
const mysql = require('mysql2');
|
|
|
|
|
|
|
|
|
|
|
|
const pool = mysql.createPool({
|
|
|
|
|
|
host: 'localhost',
|
|
|
|
|
|
user: 'root',
|
|
|
|
|
|
password: 'Skyinno251,',
|
|
|
|
|
|
database: 'appserver',
|
|
|
|
|
|
connectionLimit: 10 // 连接池最大连接数
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 设置数据库连接参数
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
user: 'system',
|
|
|
|
|
|
password: '452131wW',
|
|
|
|
|
|
connectString : "192.168.3.29:1521/orcl" // 或者直接使用 'hostname:port/sid'
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
|
|
let connection;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 获取数据库连接
|
|
|
|
|
|
connection = await oracledb.getConnection(config);
|
|
|
|
|
|
console.log('Connection established.');
|
|
|
|
|
|
|
|
|
|
|
|
// 执行查询
|
|
|
|
|
|
const result = await connection.execute('SELECT * FROM TYPE');
|
|
|
|
|
|
console.log(result.rows); // 输出查询结果
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (connection) {
|
|
|
|
|
|
// 关闭数据库连接
|
|
|
|
|
|
try {
|
|
|
|
|
|
await connection.close();
|
|
|
|
|
|
console.log('Connection closed.');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在这里挂载对应的路由
|
|
|
|
|
|
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('/post', (req, res) => {
|
|
|
|
|
|
// 通过 req.body 获取请求体中包含的 url-encoded 格式的数据
|
|
|
|
|
|
const body = req.body
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 1,
|
|
|
|
|
|
msg: 'POST请求成功',
|
|
|
|
|
|
data: body
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.delete('/delete', (req, res) => {
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 0,
|
|
|
|
|
|
msg: 'DELETE请求成功',
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 在这里挂载对应的路由
|
|
|
|
|
|
router.get('/getUser', (req, res) => {
|
|
|
|
|
|
pool.query('SELECT * FROM user', (err, rows) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error('Error querying database:', err);
|
|
|
|
|
|
res.status(500).send('Internal Server Error');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log(rows)
|
|
|
|
|
|
res.send({
|
|
|
|
|
|
status: 1,
|
|
|
|
|
|
msg: 'POST请求成功',
|
|
|
|
|
|
data: rows
|
|
|
|
|
|
})
|
|
|
|
|
|
//res.json(rows);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
pool.getConnection((err, connection) => {
|
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
connection.query('SELECT * FROM time_now', (error, results, fields) => {
|
|
|
|
|
|
connection.release(); // 释放连接
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
console.log(results);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router
|