Hi there!
I am able to get an Express server working with this tutorial:
https://paulreaney.medium.com/deploy-express-js-on-netlify-91cfaea39591
Here is my WORKING example:
https://spiffy-bubblegum-56dac1.netlify.app/.netlify/functions/api
But when I try to add Mongoose I get this:
https://kingpong-server.netlify.app/.netlify/functions/api
Are there extra steps to deploying an express server that uses mongoose to call a mongo database?
Thanks in advance!
Tim
Edit: To clarify, when I run the express server on my local it works perfectly.
1 Like
If I’m not wrong, shouldn’t this:
const challengesRouter = require("./routes/challenges/index");
const tournamentsRouter = require("./routes/tournaments/index");
app.use(express.json());
app.use(cors());
const mongoose = require("mongoose");
mongoose.connect(process.env.DATABASE_URL);
const Game = require("./models/game");
app.get("/", async (request, response) => {
console.log("hey");
response.send({ hello: "world" });
});
app.use("/.netlify/functions/api", gamesRouter);
app.use("/.netlify/functions/api", playersRouter);
app.use("/.netlify/functions/api", challengesRouter);
app.use("/.netlify/functions/api", tournamentsRouter);
be router.get()
? That’s what you’ve done in the example that works. You also need to configure your app to use the router.
I figured it out, there were two issues, both causing the function calls to take more than 10 seconds so it was timing out.
The first issue was the simplest, I hadn’t allowed any IP address to query the database on my MongoDB dashboard.
The second issue was I hadn’t set my handler up properly to interact with asynchronous functions (which mongoose uses).
Here is the code I used to fix it:
Before:
module.exports.handler = serverless(app);
After:
const handler = serverless(app);
module.exports.handler = async (event, context) => {
const result = await handler(event, context);
return result;
};
Thanks for the tip! I figured it out before, so apologies I thought I’d responded on here already!