Hi, really I need your help with this.
I am building a web app with Qwik framework, Express, Mongodb, and Netlify (also implementing netlify functions). See my site here: https://nexasoft.netlify.app/. Then I am trying to read data from Mongo Atlas so I created a function to connect this DataBase.
import { MongoClient, ServerApiVersion } from "mongodb";
// Variables de entorno
const DB_USER = import.meta.env.VITE_DB_USER;
const MONGO_HOST = `mongodb+srv://${DB_USER}:${DB_PASSWORD}@${DB_HOST}?retryWrites=true&w=majority`;
export const mongoClient = new MongoClient(MONGO_HOST, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
const clientPromise = mongoClient.connect();
const connectDB = async () => {
try {
const database = (await clientPromise).db(DB_NAME);
console.log("[db] Conectada con Γ©xito", database);
const collection = database.collection(MONGODB_COLLECTION);
const results = await collection.find({}).toArray();
return {
statusCode: 200,
body: JSON.stringify(results),
};
} catch (err) {
console.error("[db] Error", MONGO_HOST, err);
return { statusCode: 500, body: err.toString() };
}
};
export default connectDB;
Then from express I use a route to implement this function and get data for my component with a fetch request. But I have not achieve to read my database info.
app.get("/api/allnames", async (req, res) => {
const docs = await connectDB();
console.log("Call docs", docs);
res.json(docs);
});
When I run pnpm start
with my local config fetch("http://localhost:5173/api/allnames/")
, I get that it returns as response a 404 code with no documents. Then If I run pnpm build
with my production config fetch("https://nexasoft.netlify.app/api/allnames/")
, my app build correctly in my computer, then I push it to github where the Netlify deploy fails with this:
[ERROR] Could not resolve "/opt/build/repo/adaptors/express/vite.config.ts"
I also run the command, netlify dev
, witch opens the content from my site but still I get the 404 message trying to access my data from Mongo.
So I decided to install, Netlyfy functions to serverless. I created a new function get_contacts witch made my MongoDB conection and call (there I import my .env variables like, process.envβ¦). Again running with this configuration, netlify dev
or even pnpm start
, the explorer shows me, Could not proxy request. Then the terminal closed with this:
TypeError: Failed to parse URL from /.netlify/functions/get_contacts.
TypeError [ERR_INVALID_URL]: Invalid URL
I have not any idea witch could be my mistake, here my github repository: GitHub - Maikpwwq/nexasoft: ContΓ‘ctenos nos que con gusto le ayudamos, NexaSoft SAS, el futuro en soluciones de software. !Consigue una ventaja competitiva con nosotrosΒ‘ -> https://www.nexasoft.dev/