Hosting a Python or Node.js app online 24/7 for free

How To Host A Python or Node.js App 24/7 Online For Free

You may always wonder if you could host your node.js or python app for free avoiding the unnecessary costs. You can even host apps written in c, c++, html5, JavaScript, bash, etc including hosting Discord bot, Twitter bot, Telegram bot. You can host your app for free and yes, 24/7 online, 100% working!

STEP 1 – HOSTING THE PYHTON OR NODE.JS APP

First of all create an account on Replit and login to your account. Then follow the steps to host your app or bot:

PRO TIPS: Use a random username and a random project name to avoid your codes from being spoiled to the world, as free plan of replit doesn’t let you to make your projects private. Also use built-in Environment Variables of replit to store valuable variables such as database password, token, etc to protect them.

  1. Under “Create” click on the “+
    HOSTING THE PYHTON OR NODE.JS APP
  2. Select the language you used to develop your bot (basically Python or Node.js). In “title” input box, type any name. Click on “Create Repl
    HOSTING THE PYHTON OR NODE.JS APP
  3. Click on the three dots and click “upload folder” or “upload file” and upload your bot files.
  4. Make sure to rename the main file that executes first to “index.js” for Node.js language or “main.py” for Python language.

Replit projects shutdown after few minutes of inactivity, So, we need to use Flask to keep our project alive 24/7. But before that let’s jump to the next step and install all required libraries and other important things…

STEP 2 – SETTING UP FLASK IN REPLIT

As we mentioned previously, we are going to use Flask to keep our projects alive, let’s setup the Flask by following the steps:

SETTING UP FLASK IN NODE.JS

If you are using Node.js language then follow the steps below else check for the Python method:

  1. create a file named “keep_alive.js” and add the following codes in it:
    var http = require('http');
    http.createServer(function (req, res) {
    res.write("I'm alive");
    res.end();
    }).listen(8080);
  2. Now in your “index.js” on the top, add this code to include keep_alive.js function in it:
    const keep_alive = require('./keep_alive.js')
  3. Done! let’s jump to the next step.

SETTING UP FLASK IN PYTHON

If you are using Python Language, follow the steps below:

  1. create a file named “keep_alive.py” and add the following codes in it:
    from flask import Flask
    from threading import Thread
    app = Flask('')

    @app.route('/')
    def home():
    return "I'm alive"
    def run():
    app.run(host='0.0.0.0',port=8080)
    def keep_alive():
    t = Thread(target=run)
    t.start()
  2. Now in your “main.py“, on the top, add this line to import the functions:
    from keep_alive import keep_alive
  3. After the library imports, we need to execute the function by calling this:
    keep_alive()
  4. Done! let’s jump to the next step.

STEP 3 – SETTING UP NODE.JS OR PYTHON LIBRARIES / PACKAGES AND ENVIRONMENT VARIABLES

It’s important to install all required libraries to run your project. Also, using Environment Variables to protect your important variables is strongly recommended!

INSTALLING REQUIRED NODE.JS OR PYTHON LIBRARIES/ PACKAGES

So, after you are done with step 1 and step 2, try running your project once. The required libraries might install by itself. Just click on the “run” button on the top middle:
INSTALLING REQUIRED NODE.JS OR PYTHON LIBRARIES/ PACKAGES

But in case you get an error in the console, just follow the steps (note you can also use a “.txt” file and install all the packages at once using the terminal):

  1. Click on the Box icon on the left corner and then search for the libraries/packages you used in your project.
    INSTALLING REQUIRED NODE.JS OR PYTHON LIBRARIES/ PACKAGES
  2. Done! Use the same process and install all required libraries/packages in case it doesn’t install automatically

USING ENVIRONMENT VARIABLES IN REPLIT

Click on the Lock icon on the left corner and Type a variable name in “key” input box and type the secret value in “value“. Click on “Add New Secret“. Do the same for adding each new secret variable

To use these variables In PYTHON:

  1. Add this on the top of the py file where you want to use the env variables(need to be imported once):
    import os
  2. Then before using the variable add this line:
    ANYNAME = os.environ['ENVNAME']
    In “ANYNAME” use any name, you will be using the name to use the variables. In “ENVNAME” use the exact name you used in the “value” of the env.
  3. Now simply call the name you used in “ANYNAME” as a variable

To use these variables In NODE.JS:

  1. simple add the line:
    const ANYNAME = process.env['ENVNAME']
    In “ANYNAME” use any name, you will be using the name to use the variables. In “ENVNAME” use the exact name you used in the “value” of the env.
  2. Now simply call the name you used in “ANYNAME” as a variable

STEP 4 – KEEPING REPLIT PROJECT ALIVE FOR 24/7

Now we have to ensure that our replit projects gets a ping every certain amount of time, basically 3-5 minutes. So, we will be using Uptime Robot or Better Uptime . Create an account on MonoPluse (Recommended) or UptimeRobot or BetterUptime or both.

For MonoPulse, login to your account and click “Create Monitor“. Give a name and then paste the url we copied from step 2 in “URL “. Then select “Check Interval” to 5 mins or lower and keeping all other settings the same, click “Create“.

For BetterUptime, login to your account. The go to “Monitors” and click “Create Monitor“. Then paste the url we copied from step 2 in “url to monitor” and keeping all other settings the same, click “Create Monitor“.

For UptimeRobot, login to your account and click “+ Monitor“. Then in “Monitor Type” select “HTTP(s)“. In “Friendly Name” Type any name you want and in “URL (or IP)” paste the url we copied and keeping all other settings the same, click “Create Monitor

Congratulations!

You have successfully hosted your python or node.js app 24/7 online for free!

Thanks For Allowing Us To Help You 🙂
If you are confused or want to know something, then let us know in the comment box, we will reach you as soon as possible. Don’t Forget To Subscribe our Newsletter, YouTube Channel, and Like Our Facebook Page To Keep Updated With Awesome Things. Follow us on Twitter to stay updated with the latest news & changes.

Did you like it?