I am currently building a fairly basic cordova application academically. Previous experience of this has been where I have been provided with an api to a database to use in the app. I have built a database in mysql workbench and would like my Cordova app to fetch data from there. Is this possible? Any guidance would be gratefully received.
I have tried creating a server.js file within the directory which has been successful in postman to get, but not post but don’t know how I would code this into my index.js.
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '**********',
database: '*****',
insecureAuth : true
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected to the MySQL database');
});
app.use(express.json());
app.get('/Catalogue', (req, res) => {
const sql = 'SELECT * FROM Catalogue';
connection.query(sql, (err, results) => {
if (err) throw err;
res.send(results);
});
});
app.post('/Catalogue', (req, res) => {
const {Drug, Strength, Form, Pack_Size, Adult_Box_Item, Paed_Box_Item, Shelf_Location, Supplier_ID, Min_Level, Current_Level} = req.body;
const sql = `INSERT INTO Catalogue (Drug, Strength, Form, Pack_Size, Adult_Box_Item, Paed_Box_Item, Shelf_Location, Supplier_ID, Min_Level, Current_Level) VALUES ('${Drug}', '${Strength}', '${Form}', '${Pack_Size}', '${Adult_Box_Item}', '${Paed_Box_Item}', '${Shelf_Location}', '${Supplier_ID}', '${Min_Level}', '${Current_Level}')`;
connection.query(sql, (err) => {
if (err) throw err;
res.send('Item created successfully');
});
});
user24937933 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.