Skip to content

Hosting your Express app

Why is this important 💫

Before you go forward and explore Express on your own, it is important to know one last thing: how to host your app. Although hosting is not a requirement to use Express for your needs, if you ever will try to expand your website past your local machine (your computer), hosting will be unavoidable.

Hosting is basically making your app run independant of your computer (on a separate, dedicated server)

Services 🛞

To host your Express app:

We are going to use Render for this tutorial.

Step 1: Create an account on Render

Go to Render and sign up for a free account.

Step 2: Create a new file

Create a new file if you are starting from scratch, or replace your "server.js" in your project directory with the following content:

    const express = require("express");
    const app = express();
    require('dotenv').config();

    app.get('/', (req, res) => {
    if (process.env.HOSTED === 'yes') {
        res.send('Your app is hosted!');
    } else {
        res.send('Your app is running locally.');
    }
    });

    app.listen(3000, () => {
    console.log("Server is running on http://localhost:3000");
    });

.env

As you can see, we will be using a Node.Js package called dotenv.

Dotenv is a package that allows us to pass variables without changing the code. For example, in Render there will be an option to put env files. They will then be refered to in our code. You will see how it works in Step 4.

Another reason we are using it is due to GitHub. It warns the user that leaving your passwords or API codes and other sorts of keys is a danger. We want to start building a habit of using env variables so that we do not encouter any problems later on your Express journey.

You will need to install it:

npm install dotenv

Run this command in your terminal

Now, in the same directory you need to create a file called .env Put this inside of it

HOSTED=no

Step 3: Upload to Github

At this point, your working folder should look something like this:

Example

Your .js file might have a different name, but should be similar

Image title

Add a .gitignore file to the same folder put node_modules inside of it.

This will prevent hundreds of modules you installed from going into your git repository. Because of how Node works, node_modules is a very large file. When someone else want to use your code, they will only need to run npm i to get the modules, so there is no need for node_modules to be uploaded.

Now, create a new repository (it can be private or public, but preferably private). If you are not sure how to do this, check this guide out!

Step 4: Upload to Render

  1. Last step needed. Go on render and select "create new web service"

    Image title

  2. From the dropdown, select your git repository

    Image title

  3. Update build and start commands to use npm i and node *server.js*

    Image title

  4. Select instance type Free 💵

  5. In Environment Variables section, type HOSTED and a value yes

    Image title

Danger

HOSTED should be capitalised as that is what we have put in dotenv. dotenv is case sensitive

  1. Click deploy

  2. Open your render website

    Image title

If you were successful, you should see this:

Success

Image title

Successful deployment

Congratulations on setting up your first Express.js App! 🌠 🌠 ðŸŒ