I made a client-server side app using React.js and Expres.js. I also used a local database made on mysql workbench.
I want to deploy my app to app-engine on google cloud, for this purpose I exported my db and now it is on the cloud.
The problem arises when I want ot deploy the app. The client sode shows, but whenever I try to do any function the app crashes and I get an error CONNECTION REFUSED.
So far I have tried:
- using the app engine template from the documentation
- using keys
- using solutions from gemini, copilot and chatgpt
This is my app.yaml
runtime: nodejs18
env: standard
instance_class: F2
handlers:
# Catch all handler to index.html
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301
automatic_scaling:
target_cpu_utilization: 0.65
min_instances: 1
max_instances: 10
env_variables:
INSTANCE_CONNECTION_NAME: "hidden"
DB_PRIVATE_IP: ,
INSTANCE_HOST: "hidden"
DB_PORT: "3306"
DB_USER: "root"
DB_PASS: "************"
DB_NAME: "social_media"
vpc_access_connector:
name: projects/hidden
// import userRoutes from "./routes/users.js";
// import postRoutes from "./routes/posts.js";
// import likeRoutes from "./routes/likes.js";
// import commentRoutes from "./routes/comments.js";
// import relationshipRoutes from "./routes/relationships.js";
// import authRoutes from "./routes/auth.js";
// import cookieParser from "cookie-parser";
// import cors from "cors";
// import multer from "multer";
// import Express from "express";
// const app = Express();
// //middlewares
// app.use((req, res, next) => {
// res.header("Access-Control-Allow-Credentials", true);
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
// res.header("Access-Control-Allow-Headers", "Content-Type");
// next();
// });
// app.use(Express.json());
// app.use(
// cors({
// origin: "http://localhost:3000",
// })
// );
// app.use(cookieParser());
// const storage = multer.diskStorage({
// destination: function (req, file, cb) {
// cb(null, "../client/public/upload");
// },
// filename: function (req, file, cb) {
// cb(null, Date.now() + file.originalname);
// },
// });
// const upload = multer({ storage: storage });
// app.post("/api/upload", upload.single("file"), (req, res) => {
// const file = req.file;
// console.log("Received file:", file);
// res.status(200).json(file.filename);
// });
// app.use("/api/users", userRoutes);
// app.use("/api/posts", postRoutes);
// app.use("/api/likes", likeRoutes);
// app.use("/api/comments", commentRoutes);
// app.use("/api/relationships", relationshipRoutes);
// app.use("/api/auth", authRoutes);
// app.listen(8800, () => {
// console.log("API working!!");
// });
// /*const {Sequelize} = require('sequelize');
// // Create a new Sequelize instance.
// const sequelize = new Sequelize('social_media', 'root', 'password', {
// host: '/cloudsql/third-current-418819:us-central1:systems3db',
// dialect: 'mysql',
// });
// // Test the connection to the database.
// sequelize
// .authenticate()
// .then(() => {
// console.log('Connection to the database has been established successfully.');
// })
// .catch(err => {
// console.error('Unable to connect to the database:', err);
// });
// */
import userRoutes from "./routes/users.js";
import postRoutes from "./routes/posts.js";
import likeRoutes from "./routes/likes.js";
import commentRoutes from "./routes/comments.js";
import relationshipRoutes from "./routes/relationships.js";
import authRoutes from "./routes/auth.js";
import cookieParser from "cookie-parser";
import cors from "cors";
import multer from "multer";
import Express from "express";
import mysql from "mysql2/promise";
const app = Express();
// Get the environment variables
const {
INSTANCE_HOST,
DB_PORT,
DB_USER,
DB_PASS,
DB_NAME,
INSTANCE_CONNECTION_NAME,
} = process.env;
// Create a connection pool
const pool = mysql.createPool({
host: INSTANCE_HOST,
user: DB_USER,
database: DB_NAME,
password: DB_PASS,
port: DB_PORT,
socketPath: /cloudsql/${INSTANCE_CONNECTION_NAME},
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
// Now you can use the pool to query your database
app.post("/users", async (req, res) => {
try {
const [rows, fields] = await pool.execute("SELECT * FROM users");
res.json(rows);
} catch (err) {
res.status(500).json(err);
}
});
// etc.
//middlewares
app.use((req, res, next) => {
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.use(Express.json());
app.use(
cors({
origin: "http://localhost:3000",
})
);
app.use(cookieParser());
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "../public/upload");
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname);
},
});
const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
const file = req.file;
console.log("Received file:", file);
res.status(200).json(file.filename);
});
app.use("/api/users", userRoutes);
app.use("/api/posts", postRoutes);
app.use("/api/likes", likeRoutes);
app.use("/api/comments", commentRoutes);
app.use("/api/relationships", relationshipRoutes);
app.use("/api/auth", authRoutes);
const PORT = process.env.PORT || 8800;
app.listen(PORT, () => {
console.log(API working P:${PORT}!!);
});