How to host any node.js bot for FREE!!!

Looney
ShiroDiscordBot
Published in
8 min readJul 6, 2019

How to host any Node.js Discord Bot for free?

Many people have been asking me the same question over and over again.

  • How do you keep your discord bot written in node.js online 24/7?
  • How is my bots response time so fast?
  • Why is your bot so fast? etc..and etc…

But I just reply to them saying, “My bots are made by me and I know what should be the speed of the bot” or just don’t reply to that question. But today, in this blog post, I’ll be revealing the secret. I’ll be dividing this into questions. Also, this is my very first time to write a blog post. Excuse all the errors caused by me.

So to be honest, the hosting service which I use to host my node.js discord bots is Glitch it’s a web hosting cloud infrastructure running on Amazon Web Services.

So, here’s the first question which arises when I think about this topic.

1. What is Glitch.com?

Glitch.com is a free to use website which hosts HTML, CSS, JavaScript, PHP scripts running on Amazon Web Services.

According to glitch.com….

Glitch is the friendly community where everyone can discover and create the best stuff on the web.

From useful tools that solve problems at work, to cutting-edge VR experiences, smart bots, and apps that help advance important causes, Glitch is a unique community where people have built over a million projects for you to discover, with new ones are popping up every day.

If you’re new to Glitch, just explore some of the featured projects or categories to check out fun games to play or try out handy little apps. It’s like a familiar App Store, but almost everything is free and created by regular people like you.

But if you don’t find exactly what you want, that’s where the magic happens.

Every app on Glitch can be remixed. With just a click, you get your own copy of the app that you can customize, personalize, extend and build on to your heart’s content.

If you’re not a developer, don’t sweat it — you can make simple changes as easily as you edit a spreadsheet. Apps update live as you type. You can even share the project with a friend or colleague and they can work on the same code at the same time as you. It makes collaborating on code as easy as sharing in Google Docs. And if you get stuck, you can just raise your hand for help!

From students who are just seeing their first line of code, to some of the best programmers at the biggest tech companies, everybody is on Glitch and ready to help each other out.

Check back on Glitch each day, and we’ll show you cool, inspiring, useful apps and sites that you won’t see anywhere else, made by some of the most creative people on the internet.

More information about Glitch.com.

I think it’s a great tool to showcase concepts, and also allow people to use your projects and build upon them. It also helps programmers to edit their code while they are on any mobile devices.

With Glitch, you can easily create demo sand prototypes of applications written in JavaScript, from simple web pages to advanced frameworks such as React or Vue, and server-side Node.js apps.

It is built on top of Node, and you have the ability to install any npm package you want, run webpack and much more.

It’s brought to you by the people that made some hugely successful products, including Trello and Stack Overflow, so it has a lot of credibility bonuses for that.

2. Why do I think Glitch.com is great?

Glitch “clicked” for me in how it presents itself in a funny interface, but not dumbed down.

You have access to logs, the console, and lots of internal stuff.

Also, the concept of remixing so prominent in the interface makes me much more likely to create lots of projects there, as I never have to start from a clean slate.

You can start diving into the code without losing time setting up an environment, version control, and focus on the idea, with an automatic HTTPS URL and a CDN for the media assets.

Also, there’s no lock-in at all, it’s just Node.js (or if you don’t use server-side JavaScript, it’s just HTML, JS, and CSS)

This is how to the homepage of Glitch.com looks like when you are not logged in or it’s your first-time visit to the website.

I’ll speak about Glitch.com more in my other blog, but for now, that’s just the introduction to Glitch.com

Hosting Discord.js or any Node.js for free.

I’ve finally found it. I found a free hosting that you guys can enjoy and put your bots on, that’s not like the most complicated 4D puzzle in the world to get functional, and that actually supports not only the most recent version of node but also saving files locally so you can have a simple bot with persistence without so much hassle you’d be better off waiting until you had a job to get hosting.

So OH MY GOD!!, let’s get to it right now because I’m so excited. I mean there are tiny little things you have to be weary of, but it’s, like, Almost Paradise. Let’s knock on heaven’s door.

1. Getting Code Running

Ok like, have you ever seriously looked into getting a bot up on Heroku? God, it’s a mess of setups and configurations and holy crap what the hell is a worker and a procfile?????

Glitch.com has none of this. Getting code up and running is as simple as:

  • Go to Glitch.com
  • Completely ignore the childish drawings of a 3-year-old on the front page (trust me on this one, don’t look)
  • Click on Start a New Project, then Create a Node App

You’re… well, technically you’re done.

That’s right. You can start coding right away, technically. The default project is an express.js website. Don’t delete that yet, we’ll still need some of that code later on. Because, as simple as this might be and, well, really is actually, there’s still a few things that we need to set up, including making sure the project stays online and securing it against prying eyes.

2. Create an account.

In order to not ever lose access to your code, the first thing you need to do is to create an account.

  • Click Sign In at the top-right of the page
  • Choose either Github or Facebook to log in. In the current version of glitch.com, you can sign in using Emails too.

Yeah ok, the project is now yours.

Toldja this shit was simple.

3. Configure the Project

So here are a few things about the project that we need to configure, for a few reasons.

First off, Secure the project. By default, anyone with your project’s name can access your code directly. They can’t edit it but they can snoop in and look at your code. And, btw, I haven’t found a way to un-share someone that’s viewed your project yet (I’m talking to Glitch to get that fixed).

  • Click on the project name and you can see below the project description a lock icon with a checkbox.
  • Click on click on the checkbox to make the project private.

You can still invite people to view and collaborate later, with the link provided.

WARNING: If you make your project private, people can snoop if you accidentally give out the project name and see the tokens in your env file, if you leave it public they can view your code, but cannot view any tokens inside the env file.

The next thing is, Name the project. Now, projects work through express.js whether you really want to or not. Later on you can learn to make a dashboard but for now, we just need to set it up to keep it alive.

  • Click on the project name at the top-left of the screen.
  • In the pop-up click on the name at the top (slightly counter-intuitive, but yeah that’s how you rename it)
  • The name you choose (it must be unique and not taken by anyone else) will be your “site’s” subdomain address.

While you’re at it, give it a description if you really want to.

Finally, we need to Disable some auto-save features. Glitch automatically saves the file, quite literally, on every keypress you make. And restarts it. This is not only slightly visually annoying but also damaging to bots — the Discord API will reset your bot’s token if you login 1000 times in a day. That means if you type 1000 characters in your code, as it is. QUITE an issue.

  • Create a new file in the project, and call it “watch.json”.
  • Paste in the following code in it:
{“install": {"include": ["^package\\.json$","^\\.env$"]},"restart": {"exclude": ["^public/","^dist/"],"include": ["\\.js$","\\.json"]},"throttle": 900000}

This number, “900000”, means that every 15 minutes, if any files have changed, the bot will restart. Now there is a caveat here, which is that this also means any change you do in the bot will not take effect (will not reboot) until, up to, 15 minutes. But hey. It’s free, let’s not look a gift horse in the mouth!

Oh one last thing for you crazy people with light-sensitive eyes (aka dark theme users) : click on your avatar at the top right, then click on “Change Theme”. I know, right? You’re welcome!

4. Keeping the project “alive”

Alright so, like, Glitch is made to be a web hosting really, and will “sleep” after 5 minutes if it receives no HTTP request. However, there is a very convenient way to keep it alive, which is actually provided by the app itself — the express.js module is pre-installed, and all you need to do is to “ping” it every 5 minutes to make sure it doesn’t sleep. These lines of code in your project (either the main file or any module you call on bootup) should do the trick for now:

const http = require('http');const express = require('express');const app = express();app.get("/", (request, response) => {console.log(Date.now() + " Ping Received");response.sendStatus(200);});app.listen(process.env.PORT);setInterval(() => {http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);}, 280000);

*From the above code, copy everything except ```.

What does this do? Keeps an express.js server alive, which does not really affect your project in and of itself, and pings itself every 5 minutes, so it never shuts off. Awesome.

INFO: For best results, have an outside source pinging the project address as well, glitch.com suggests using Uptime Robot or .

5. Package.json

There are 2 things that you much change in the project’s package.json file in order to ensure that your project will actually work.

First, you must provide for a node.js version if your project requires a higher version of node (for instance, 10.5.0). This is done with the engines key, as such: “engines”: { “node”: “10.5.0” }

Second, you must provide for the start script. A lot of us just generally configure the main: index.js key and this is not sufficient. You must provide for a start script:

{"name": "samplejson","version": "1.0.0","description": "A sample package.json","main": "node index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1","start": "node index.js"},"engines": { "node": "10.5.0" },"repository": {"type": "git","url": ""},"author": "Discord Bot Developments","license": "MIT","bugs": {"url": ""},"homepage": "","dependencies": {"discord.js": "^11.4.5","enmap": "^0.3.2","moment": "^2.18.1","moment-duration-format": "^1.3.0","express": "^4.15.5"}}

*From the above code, copy everything except *.

Another change is that your config.jsonfile or config.js the file is insecure if you share your project. The easiest way to fix this is to use environment variables. Open the .env file, and add the following line:

TOKEN=MzUzOTUxODYwOTA3OTY2NDY0.DI3K3w.VN1Gvsl7CSh2IYIELJDJAFejH4w

INFO: Obviously use your real token, duh! And the token above is fake.

You can then access this from anywhere using process.env.TOKEN, you would do the following in config.js:

“token”: process.env.TOKEN,

Conclusion

If you are looking for Continuous Deployment of your bot and which is free, use Glitch.com is a safe, secure and great platform

--

--

Looney
ShiroDiscordBot

The main developer of Shiro Beta and Shiro Bot.