Deploying Website

Deploying Website
0

#1

I have a MongoDB/Node.js website up and running. I think it’s the nicest thing I’ve ever built, but I’m afraid to even try deploying it though because I don’t know how, tbh.

I can deploy to heroku, it’s easy, but configuring the DB is what’s confusing me. I tried on a previous project with using MLab, but the app doesn’t work.

Can someone give me a run down of this?
Where should I buy the domain name? Hosting?

I’ve bought a domain name from godaddy before for a static website. I hosted it on AWS S3, but when I pointed the domain from godaddy to AWS, it threw an <iframe> over my website, and killed its performance/messed with functionality.

I’m not trying to run into that problem again. Just need a smooth way to get everything up and running. This isn’t so much a personal project, I plan on having users. So I guess I’m looking for advice on deploying a “professional” website.

Thank you!


#2

I would check out this guide for aws.
It doesn’t use MongoDB but it may be of some help.

In my opinion Heroku would still be a good option, but really there are a ton of factors because there are so many different options depending on the needs of your application.

I think another option would be to use godaddy again but I believe you would have to get VPS hosting at minimum to have root access to use nodejs. Do not quote me on that one, if you go that way call them first to verify.


#3

This is more likely to be S3 displaying your html in an iframe. Domain name records can be confusing, but it’s not really difficult - here are Heroku’s instructions: https://devcenter.heroku.com/articles/custom-domains

If you make an Mlab free-tier database and add a user for it, you should get a ‘connection string’ that you can pass to your app in place of the address for your local db. The only tricky thing about it is that you need it to be a secret, because it includes your password. The easiest way to do that is to set it as an environment variable and refer to it from your code - you can set Heroku environment variables from their CLI tool.

Personally, I had to overcome my ‘fear’ of making things live by realising that I wasn’t going to break the internet, nobody would even notice if I did it wrong and, if I did, I could just delete everything and start over.


#4

With regards to deploying mLab, I have done it several times from Heroku. It was a little confusing at first, but makes sense once you get used to it.

First of all, you need your mLab. Create your mLab. You want the free/sandbox tier. Once that is created, you will go to the “Users” section and make a user/password. This is different than your mLab account - this is so you can have different users accessing different things and control what they can access. Click on “Add database user” and add a user. After you do this, above you will see a box with the following information:

To connect using the mongo shell:
mongo ds235778.mlab.com:35778/asdf -u <dbuser> -p <dbpassword>
To connect using a driver via the standard MongoDB URI (what's this?):

mongodb://<dbuser>:<dbpassword>@ds235778.mlab.com:35778/asdf

(“asdf” was just the name I gave the DB.)

This is the information you will need for heroku.

Go to heroku. I found it easiest to create environment variables to keep these values. In node, you can use the dotenv package that allows you to save your “secret” information in a file called .env and then if you include that file in your .gitignore, no one can see it. This is great for passwords, API keys, etc. It also makes it extremely easy to port to heroku and set all those up as environment variables.

So, in my source files, I have a .env file where I have something declared like:

MONGO_DB=mongodb://user123:[email protected]:35778/asdf

This is the same string that mLab gave me except that I have substituted in the user name and pwd, assuming that they are “user123” and “pwd123”. Remember, these are not your mLab account username and password, but for the user you set up for this specific database.

In your application, you have:

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect(process.env.MONGO_DB, ...

Those variables from the .env file are automatically loaded into the `process.env’ object when you run the code. At this point, you should be able to run your code locally and access the mLab data.

The nice thing is that heroku also has a process.env object, but we don’t access it with a .env file (we don’t want that stored in the git) but through “config variables”. Go to your heroku app and select “settings”. Select “Reveal Config Variables”. From there, you simply enter anything you had in your .env file. For example, I add a line with a key (on the left) of “MONGO_DB” and a value (on the right) of “MONGO_DB=mongodb://user123:[email protected]:35778/asdf”.

I think that’s basically it. Of course, you can skip the environment variables part and just hard code your mLab string straight into your code, but I think that’s bad practice because people can look at your github and get those usernames and passwords. It’s a little extra typing to hide them, but not much.

Hope this helps.