Context:
I want to show some records from mongoDB to my front-end chrome extension(iNotes). So, i need backend that will communicate with my mongoDB.
Problem:
I'm building a serverless function using Netlify Functions to connect to my MongoDB database. The function works perfectly in local development using netlify dev, but in production (after deploying with netlify deploy --prod), I get a 504 Gateway Timeout
So, My setup is:-
- functions/getFeatureRequests.js
- Using mongodb driver
- MongoDB Atlas free-tier cluster
- Netlify Free Plan
const { MongoClient } = require('mongodb');
const uri = "uri_here";
exports.handler = async function () {
const client = new MongoClient(uri);
try {
await client.connect();
const db = client.db("i_notes_db");
const collection = db.collection("feature_requests");
const data = await collection.find().toArray();
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
} finally {
await client.close();
}
};
I have some question
- Is there any way to reliably use access MongoDB with Netlify Functions on the free plan?
- Would Netlify Background Functions or another host (Render, Vercel, etc.) be more reliable for MongoDB-backed APIs?
My uses case:
I want to show some records from mongoDB to my front-end chrome extension(iNotes). So, i need backend that will communicate with my mongoDB.