Deploy the tiniest nodejs server in repl.it
Heiker
Posted on February 12, 2021
Pueden leer la versión en español aquí.
I recently learned that repl.it has support for web servers. I know, I'm late to the party. In my defense I've never needed (or wanted) one. Anyway, we are going to learn how we can deploy a microservice-ish type of deal using repl.it.
The boilerplate
Let's say you created a fresh node
repl.it. By default you'll have an index.js
file created and ready to go. In that file we are going to place the most simple code we can think of to test our server. Here it is.
module.exports = async function(request, response) {
return 'Hello!';
}
Now we need something else.
The server
The wonderful team of vercel has exactly what we need to run this thing, it is package called micro. You can install it using the menu on the side bar. Click on the little icon that looks like a box, and then search for micro. Select the package and click the plus sign.
The config
We have the business logic of our server ready, we have the server itself, now we need to tell repl.it how to run it.
Create a new file called .replit
with the following content.
run = 'node_modules/.bin/micro -l tcp://0.0.0.0:3000 index.js'
That file, specifically the run
property, can tell repl.it what command we want to execute when we press the big button that says "run".
And that commands what it does is this: First reach for the micro executable (which is the one that actually does the setup for the server), tell it to listen to this url tcp://0.0.0.0:3000
and that index.js
is the entrypoint for our server.
The Grand Finale
Now all you have to do is press that "run" button and you're done.
You can check out a working example here: repl - FoolishBurdensomeLine. And the wonderful content is serving is here: https://FoolishBurdensomeLine.vonheikemen.repl.co
Conclusion
Congrats! You have created a web server!
Do note that micro
really does honor its name. It really doesn't do much. All it does is give you the request so you can return a response. If you need routing, authentication or handle cookies or whatever, search in this repo: Awesome micro. If you find yourself wanting lots of those packages consider using a full-featured framework instead.
Sources
Thank you for your time. If you find this article useful and want to support my efforts, consider leaving a tip in ko-fi.com/vonheikemen.
Posted on February 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.