react app
This commit is contained in:
BIN
backend/.DS_Store
vendored
Normal file
BIN
backend/.DS_Store
vendored
Normal file
Binary file not shown.
20
backend/Dockerfile
Normal file
20
backend/Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
# Use the official Node.js 20 image as the base image
|
||||
FROM node:20
|
||||
|
||||
# Set the working directory in the container
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the package.json and package-lock.json files to the container
|
||||
COPY package*.json ./
|
||||
|
||||
# Install the dependencies
|
||||
RUN npm install
|
||||
|
||||
# Copy the rest of the application's source code to the container
|
||||
COPY . .
|
||||
|
||||
# Expose the port that the application listens on
|
||||
EXPOSE 3000
|
||||
|
||||
# Start the Node.js application
|
||||
CMD [ "npm", "start" ]
|
||||
1180
backend/package-lock.json
generated
Normal file
1180
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
backend/package.json
Normal file
19
backend/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.20.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"mysql2": "^3.4.5",
|
||||
"nodemon": "^2.0.22"
|
||||
}
|
||||
}
|
||||
99
backend/server.js
Normal file
99
backend/server.js
Normal file
@@ -0,0 +1,99 @@
|
||||
const express = require("express");
|
||||
const mysql = require("mysql2");
|
||||
var cors = require("cors");
|
||||
const bodyParser = require("body-parser");
|
||||
|
||||
// Create the Express app
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// Create a connection to the MySQL database
|
||||
const mysqlConfig = {
|
||||
host: process.env.DB_HOST || "db",
|
||||
port: process.env.DB_PORT || "3306",
|
||||
user: process.env.DB_USER || "root",
|
||||
password: process.env.DB_PASSWORD || "pass123",
|
||||
database: process.env.DB_NAME || "appdb",
|
||||
};
|
||||
|
||||
let con = null;
|
||||
const databaseInit = () => {
|
||||
con = mysql.createConnection(mysqlConfig);
|
||||
con.connect((err) => {
|
||||
if (err) {
|
||||
console.error("Error connecting to the database: ", err);
|
||||
return;
|
||||
}
|
||||
console.log("Connected to the database");
|
||||
});
|
||||
};
|
||||
|
||||
const createDatabase = () => {
|
||||
con.query("CREATE DATABASE IF NOT EXISTS appdb", (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
console.log("Database created successfully");
|
||||
});
|
||||
};
|
||||
|
||||
const createTable = () => {
|
||||
con.query(
|
||||
"CREATE TABLE IF NOT EXISTS apptb (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))",
|
||||
(err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
console.log("Table created successfully");
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// GET request
|
||||
app.get("/user", (req, res) => {
|
||||
databaseInit();
|
||||
con.query("SELECT * FROM apptb", (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send("Error retrieving data from database");
|
||||
} else {
|
||||
res.json(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// POST request
|
||||
app.post("/user", (req, res) => {
|
||||
con.query(
|
||||
"INSERT INTO apptb (name) VALUES (?)",
|
||||
[req.body.data],
|
||||
(err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send("Error retrieving data from database");
|
||||
} else {
|
||||
res.json(results);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
app.post("/dbinit", (req, res) => {
|
||||
databaseInit();
|
||||
createDatabase();
|
||||
res.json("Database created successfully");
|
||||
});
|
||||
|
||||
app.post("/tbinit", (req, res) => {
|
||||
databaseInit();
|
||||
createTable();
|
||||
res.json("Table created successfully");
|
||||
});
|
||||
|
||||
// Start the server
|
||||
app.listen(3000, () => {
|
||||
console.log("Server running on port 3000");
|
||||
});
|
||||
Reference in New Issue
Block a user