You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

// testing :
// https://www.npmjs.com/package/mysql
const http = require ('http'); // to create server
const url = require('url'); // to parse url
const BASIC_API_PORT = 3998;
const DB_HOST = "127.0.0.1",
DB_PORT = "3306",
DB_USR = "root",
DB_PWD = "root",
DB_NAME = "nodeapi";
const mysql = require('mysql');
const pool = mysql.createPool({
host: DB_HOST,
port: DB_PORT,
user: DB_USR,
password:DB_PWD,
database: DB_NAME,
connectionLimit: 5,
});
response_text = '';
const app = http.createServer(
async (req, res) => {
const parsedURL = url.parse(req.url, true);
if (req.method === 'GET' && parsedURL.pathname.startsWith('/api/tables'))
{
console.log('requesting table list');
pool.getConnection(function (err, conn) {
conn.query("SHOW tables", function (err, rows) {
console.log(rows);
response_text += rows.length + " table" + (rows.length > 1 ? "s" : "") + " found";
//console.log(response_text);
if(rows.length > 0)
{
Object.keys(rows).forEach(function(key) {
if(key !== 'meta')
{
var row=rows[key];
response_text+= "\n - "+row['Tables_in_'+DB_NAME];
}
});
}
});
res.statusCode = 200;
res.end(response_text);
response_text ="";
});
} else {
res.statusCode = 400;
res.end("API Endpoint Not Supported");
}
}
);
app.listen(BASIC_API_PORT);
console.log('mysql test api started');