First commit for api

This commit is contained in:
yiekheng 2025-03-27 17:32:20 +08:00
commit cffab8977e
7 changed files with 1242 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules
/test

13
config.js Normal file
View File

@ -0,0 +1,13 @@
// config.js
const dbConfig = {
mysqldb: {
host: '192.168.0.170',
port: 3306,
database: 'sunnymh_mysql',
user: 'sunnymh_mysql',
password: 'hengserver'
}
};
module.exports = dbConfig;

13
db.js Normal file
View File

@ -0,0 +1,13 @@
const config = require('./config');
class DB {
constructor() {
this.config = null;
}
loadConfig() {
this.config = config;
}
}
module.exports = DB;

45
db_api.jsx Normal file
View File

@ -0,0 +1,45 @@
const express = require('express');
const MySQLDB = require('./mysqldb');
const app = express();
const port = 4000;
app.use(express.json()); // Middleware to parse JSON
// Initialize MySQL database connection
const mySQLDB = new MySQLDB();
async function initializeDatabases() {
try {
await mySQLDB.createConnection();
} catch (error) {
console.error('Failed to initialize databases:', error);
process.exit(1);
}
}
app.get('/Pic', async (req, res) => {
const mangaId = req.params.id;
try {
const sqlQuery = `SELECT * FROM manga_table WHERE manga_id = ${mySQLDB.connection.escape(mangaId)}`;
const mySQLResults = await mySQLDB.query(sqlQuery);
res.json({ mySQLResults });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/insert', async (req, res) => {
const { mangaId, mangaName } = req.body;
console.log(mangaId, mangaName);
});
// Start server after initializing databases
initializeDatabases().then(() => {
app.listen(port, () => {
console.log(`API server listening at http://localhost:${port}`);
});
});

49
mysqldb.js Normal file
View File

@ -0,0 +1,49 @@
const DB = require("./db");
const mysql = require('mysql2');
class MySQLDB extends DB {
constructor() {
super();
this.connection = null;
}
async createConnection() {
this.loadConfig();
const { mysqldb } = this.config;
this.connection = mysql.createConnection({
host: mysqldb.host,
port: mysqldb.port,
user: mysqldb.user,
password: mysqldb.password,
database: mysqldb.database,
connectTimeout: 0 // disable timeout
});
await this.connection.connect();
}
async closeConnection() {
if (this.connection) {
await this.connection.end();
this.connection = null;
}
}
async query(query) {
if (!this.connection) {
throw new Error('Database not connected');
}
const results = await new Promise((resolve, reject) => {
this.connection.query(query, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
return results;
}
}
module.exports = MySQLDB;

1103
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "sunnymh-db",
"version": "1.0.0",
"main": "db_api.jsx",
"scripts": {
"start": "node db_api.jsx",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.21.2",
"mongodb": "^6.15.0",
"mysql2": "^3.14.0"
}
}