DEV Community

loading...

Deploy the tiniest nodejs server in repl.it

vonheikemen profile image Heiker Updated on ・2 min read

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!';
}
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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

Discussion (1)

pic
Editor guide
Collapse
vonheikemen profile image
Heiker Author

So I'm going to leave here a more complete example.

This is a "full stack" app: micro-quote-machine.

It doesn't have a template engine setup, but it does handle routing and static assets.