0

I'm creating a website, and currently for a feature I need it to connect to my mongoDB database, is there any way to do this? As far as I've seen it's not possible to require the mongoose module, can I somehow work around this/connect to mongoDB in a different way?

Thanks for reading!

MadMisty
  • 1
  • 4
  • 1
    You can only connect directly from your backend. One reason for this is that (frontend) JS code is visible to the user, which would include the DB credentials. What you do instead is write a backend using for instance node and express with an API endpoint like `/images` or whatever, then request that from the frontend (using fetch) –  Aug 27 '21 at 16:51
  • Note that Google's firebase for instance allows a direct frontend connection, so it depends on the kind of data you're going to store. Sensitive data should always run through server-side validation / authentication. –  Aug 27 '21 at 16:54
  • Thanks for the comments, I'll look into that! – MadMisty Aug 27 '21 at 17:13
  • @ChrisG Thanks for the tip, but I'm still kind of stuck, I can't find any info about how I can make sure something runs on the backend and not front? (in github pages) Should I just start making a script in a separate folder inside my repo and it'll be backend? – MadMisty Aug 27 '21 at 18:00
  • [I don't think you can run backend code on github pages](https://stackoverflow.com/questions/15718649/how-to-publish-a-website-made-by-node-js-to-github-pages); you need actual hosting. If you create a node/express app, you can push it to a free heroku account. –  Aug 27 '21 at 19:33

0 Answers0