Skip to content

Instantly share code, notes, and snippets.

@gwhitcher
Created April 21, 2022 01:16
Show Gist options
  • Save gwhitcher/045cf1b9a368c2f7f4dcc1b11252e66d to your computer and use it in GitHub Desktop.
Save gwhitcher/045cf1b9a368c2f7f4dcc1b11252e66d to your computer and use it in GitHub Desktop.
CRUD with nodejs, MySQL, and express
//requires
var mysql = require('mysql');
const express = require("express");
const app = express();
const { apiHost, apiPort, apiKey, mysqlSettings } = require('./config.json');
//web server
app.listen(apiPort, apiHost, () => {
console.log("Application started and listening on http://" + apiHost + ":" + apiPort);
});
//mysql
var pool = mysql.createPool({
connectionLimit: mysqlSettings.connectionLimit,
host: mysqlSettings.host,
user: mysqlSettings.user,
password: mysqlSettings.password,
database: mysqlSettings.database
});
//create
app.get("/insert/:table/:authkey", (req, res) => {
let table = req.params.table;
let authkey = req.params.authkey;
if (authkey !== apiKey) {
res.send('API Key is invalid.')
} else {
let keyString = '';
let valString = '';
for (var key in req.query) {
keyString += key + ', ';
valString += req.query[key] + ', ';
}
keyString = keyString.slice(0, -2);
valString = valString.slice(0, -2);
pool.getConnection(function (err, connection) {
connection.query(
"INSERT INTO " + table + " (" + keyString + ") VALUES (" + valString + ")",
function (err, results, fields) {
if (err) {
console.log(err);
res.send();
} else {
res.send(JSON.stringify(results));
}
}
);
});
}
});
//read
app.get("/select/:table/:authkey", (req, res) => {
let table = req.params.table;
let authkey = req.params.authkey;
if (authkey !== apiKey) {
res.send('API Key is invalid.')
} else {
pool.getConnection(function (err, connection) {
connection.query("SELECT * FROM " + table, function (err, rows) {
connection.release();
if (err) {
console.log(err);
res.send();
} else {
res.send(JSON.stringify(rows));
}
});
});
}
});
//update
app.get("/update/:table/:id/:authkey", (req, res) => {
let table = req.params.table;
let id = req.params.id;
let authkey = req.params.authkey;
if (authkey !== apiKey) {
res.send('API Key is invalid.')
} else {
let updateString = '';
for (var key in req.query) {
updateString += key + "='" + req.query[key] + "', ";
}
updateString = updateString.slice(0, -2);
pool.getConnection(function (err, connection) {
connection.query(
"UPDATE " + table + " SET " + updateString + " WHERE id = " + id + ";",
function (err, results, fields) {
if (err) {
console.log(err);
res.send();
} else {
res.send(JSON.stringify(results));
}
}
);
});
}
});
//delete
app.get("/delete/:table/:id/:authkey", (req, res) => {
let table = req.params.table;
let id = req.params.id;
let authkey = req.params.authkey;
if (authkey !== apiKey) {
res.send('API Key is invalid.')
} else {
pool.getConnection(function (err, connection) {
connection.query(
"DELETE FROM " + table + " WHERE id='" + id + "'",
function (err, results, fields) {
if (err) {
console.log(err);
res.send();
} else {
res.send(JSON.stringify(results));
}
}
);
});
}
});
{
"apiHost": "localhost",
"apiPort": 4000,
"apiKey": "W7S5Lk8JMBb457e9JRFbRzCRHHB5Zh5N3vnnLLhDuKPDLBzXWBSsfNJ8rUnbKaRuCFp2", //set this to any random string to use for your api endpoint
"mysqlSettings": {
"connectionLimit": 10,
"host": "localhost",
"user": "someuser",
"password": "somepassword",
"database": "somedatabase"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment