Backend user query
made express func to create user, read user table with url,
This commit is contained in:
@@ -1,16 +1,58 @@
|
|||||||
import express, { json } from "express";
|
import express, { json } from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
const app = express();
|
import mysql from "mysql2";
|
||||||
|
|
||||||
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(json());
|
app.use(json());
|
||||||
|
|
||||||
// sample route
|
//TODO: Connect with the database:
|
||||||
app.get("/api/test", (req, res) => {
|
const db_con = mysql.createConnection({
|
||||||
res.json({ message: "Hello from server!" });
|
host: "localhost",
|
||||||
|
user: "root",
|
||||||
|
password: "12345678",
|
||||||
|
database: "marketplace",
|
||||||
|
enableKeepAlive: true, // false by default.
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3030, () => {
|
db_con.connect((err) => {
|
||||||
console.log("\tRunning Backend:");
|
if (err) {
|
||||||
console.log("\x1b[36m \tLocal:\thttp://localhost:3030/ \x1b[0m");
|
console.error("Database connection failed: " + err.stack);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("Connected to MySQL database.");
|
||||||
|
});
|
||||||
|
|
||||||
|
//TODO: Create a users:
|
||||||
|
app.post("/create_users", (req, res) => {
|
||||||
|
const data = req.body;
|
||||||
|
db_con.query(
|
||||||
|
`INSERT INTO Users (Name, Email, UCID, Password, Phone, Address)
|
||||||
|
VALUES ('${data.name}', '${data.email}', '${data.UCID}', '${data.password}', '${data.phone}', '${data.address}');`,
|
||||||
|
);
|
||||||
|
db_con.query(
|
||||||
|
`INSERT INTO UserRole (Role)
|
||||||
|
VALUES ('${data.role}');`,
|
||||||
|
);
|
||||||
|
console.log(data);
|
||||||
|
res.send();
|
||||||
|
});
|
||||||
|
|
||||||
|
//TODO: Fetch all users data:
|
||||||
|
app.get("/fetch_all_users", (req, res) => {
|
||||||
|
db_con.query("SELECT * FROM Users;", (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
console.error("Errors: ", err);
|
||||||
|
return res.status(500).json({ error: "\nCould not fetch users!" });
|
||||||
|
}
|
||||||
|
res.json({ Users: data });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//TODO: Fetch One user Data:
|
||||||
|
//TODO: Update A uses Data:
|
||||||
|
//TODO: Delete A uses Data:
|
||||||
|
|
||||||
|
app.listen(3030, () => {
|
||||||
|
console.log("Running Backend on http://localhost:3030/");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -41,5 +41,3 @@ func main() {
|
|||||||
fmt.Printf("Similarity with product %d: %f\n", i, sim)
|
fmt.Printf("Similarity with product %d: %f\n", i, sim)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user