generated from lucien/api-template
Created two API endpoints.
This commit is contained in:
parent
00e78c2dff
commit
8a61aaa68f
8 changed files with 854 additions and 203 deletions
10
api/hello.js
10
api/hello.js
|
@ -1,16 +1,8 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cookieParser = require('cookie-parser');
|
|
||||||
const cors = require('cors');
|
|
||||||
const jwt = require('jsonwebtoken');
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.use(express.json());
|
|
||||||
router.use(cookieParser());
|
|
||||||
router.use(cors());
|
|
||||||
|
|
||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
res.send('Hello World!');
|
res.send('Hello World!');
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
18
api/shareholders.js
Normal file
18
api/shareholders.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const { getConnection, getShareholders } = require("../libs/mysql.js")
|
||||||
|
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
const connection = await getConnection()
|
||||||
|
const shareholders = await getShareholders(connection)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
if (!shareholders[0]) {
|
||||||
|
return res.status(500).send({message: "There are no shareholders in the databse."})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(shareholders)
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
17
api/shares.js
Normal file
17
api/shares.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const { getConnection, getShares } = require("../libs/mysql.js")
|
||||||
|
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
const connection = await getConnection()
|
||||||
|
const shares = await getShares(connection)
|
||||||
|
connection.end()
|
||||||
|
if (!shares[0]) {
|
||||||
|
return res.status(500).send({message: "There are no shares in the databse."})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(shares)
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"port": 3000
|
"port": 3000
|
||||||
}
|
}
|
||||||
|
|
1
index.js
1
index.js
|
@ -4,6 +4,7 @@ const path = require("path");
|
||||||
const config = require("./config");
|
const config = require("./config");
|
||||||
const cookieParser = require("cookie-parser");
|
const cookieParser = require("cookie-parser");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
|
require("dotenv").config()
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = config.port || 3000;
|
const port = config.port || 3000;
|
||||||
|
|
47
libs/mysql.js
Normal file
47
libs/mysql.js
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
const mysql = require("mysql");
|
||||||
|
|
||||||
|
function getConnection() {
|
||||||
|
return mysql.createConnection({
|
||||||
|
host: process.env.MYSQL_HOST,
|
||||||
|
user: process.env.MYSQL_USER,
|
||||||
|
password: process.env.MYSQL_PASSWORD,
|
||||||
|
database: process.env.MYSQL_DB,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getShares(connection) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Share`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getShareholders(connection) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Shareholder`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getConnection,
|
||||||
|
|
||||||
|
getShares,
|
||||||
|
|
||||||
|
getShareholders,
|
||||||
|
}
|
||||||
|
|
956
package-lock.json
generated
956
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -14,10 +14,12 @@
|
||||||
"axios": "^1.6.5",
|
"axios": "^1.6.5",
|
||||||
"cookie-parser": "^1.4.6",
|
"cookie-parser": "^1.4.6",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.4.7",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"fs": "^0.0.1-security",
|
"fs": "^0.0.1-security",
|
||||||
"https": "^1.0.0",
|
"https": "^1.0.0",
|
||||||
"jsonwebtoken": "^9.0.2"
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"mysql": "^2.18.1",
|
||||||
|
"nodemon": "^3.1.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue