Hello community!
I created a simple API locally which accesses a MySQL database also located locally. To try it in “production”, I dumped the database to Clever Cloud. I conducted some tests on my local API, accessing this database on Clever Cloud, and it worked out. I deployed the code here on Render, set environment variables, and it seems to have gone well. However, when I try to access some endpoints, it ends up with a timeout
Here some code:
package.json
{
"name": "bestbreedmatch-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.js",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"mysql2": "^3.2.3"
},
"devDependencies": {
"nodemon": "^2.0.22",
"dotenv": "^16.0.3"
}
}
index.js (entry point in the the root of the project “/”)
// Environment
import _ from "./env.js";
import express from "express";
import { setLanguage } from "./src/middlewares/language.middleware.js";
import { requests } from "./src/middlewares/requests.middleware.js";
import indexRoutes from "./src/routes/index.routes.js";
import familiesRoutes from "./src/routes/families.routes.js";
import breedsRoutes from "./src/routes/breeds.routes.js";
import associationsRoutes from "./src/routes/associations.routes.js";
import traitsRoutes from "./src/routes/traits.routes.js";
import languagesRoutes from "./src/routes/languages.routes.js";
import bodyParser from "body-parser";
const app = express();
const port = process.env.PORT || 3000;
console.log("port:", port);
// Middlewares
app.use(setLanguage);
app.use(requests);
// Manage JSON responses
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Routes
app.use(indexRoutes);
app.use(familiesRoutes);
app.use(breedsRoutes);
app.use(associationsRoutes);
app.use(traitsRoutes);
app.use(languagesRoutes);
app.listen(port, () => {
console.log(`server listening on port ${port}`);
});
db.js (database connection in “/src/”:
import { createPool } from "mysql2/promise";
const { DB_HOST, DB_USER, DB_PASSWORD, DB_PORT, DB_NAME } = process.env;
const productionPoolData = `mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`;
const poolData =
process.env.NODE_ENV === "production"
? productionPoolData
: {
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
port: DB_PORT,
database: DB_NAME,
};
console.log("poolData", poolData);
export const pool = createPool(poolData);
I guess I should access here: https://bestbreedmatch-api.onrender.com:10000/families
Any clue will be more than appreciated! Thanks beforehand