简体   繁体   中英

Deploying Express server with Mongoose to Netlify

I am able to deploy an Express server with netlify using this tutorial: https://paulreaney.medium.com/deploy-express-js-on-netlify-91cfaea39591

When I add Mongoose to the server I get a crash notification and a timeout.

服务器超时

I then tried installing mongodb-client-encryption but same error.

Is it possible to have an express server that calls a MongoDB collection hosted on Netlify?

If there is other information I can share to aid with this please tell me and I will!

Thanks Tim

Edit: To clarify, when I run the express server on my local it works perfectly.

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM