Eon.js - My new little express competitor

A couple of days ago, I was sitting at my desk with absolutely no idea what project not to finish next. A moment nearly every developer can relate to - you need some kind of project, but you don't expect to ever finish it anyways.

So I started working on a little express app. And the very first annoying thing was just how long it takes to install that library, partially because of my slow network, and partially because npm has to go off and fetch 30 dependencies, one after another. So I thought - can't I do this my self?

I already knew how to create a server using the vanilla HTTP library, so I set out to create a little helper library, rather like express, for myself. I put a rather arbitrary limitation on myself: The library had to have no dependencies at all.

Initially, I wanted to call it "turbo.js", but since that name was already taken, I settled on calling it "eon.js" and using "eonjs" as a package name.

First, I set out to define the architecture of my framework: The index.js file exports a factory function that creates a new EonWebEngine instance. One can then use the get and post methods to register a path on the Server. Unlike in express, the handler isn't passed directly to the registrar function: The get and post methods will return an object of the Path class, on which a listener can be registered using different methods. For example the .text method of a path will invoke the callback passed to it with the request and response objects, and send whatever text is returned from the callback to the client, while the .hook method simply invokes the callback and expects the handler to do all the sending of data.

So, a simple webserver using eon might look like this:

// Note how the port is passed directly to the constructor function
const eon = require('eonjs')(8080);
eon.get('/').text(_ => 'Hello World');
eon.listen(port => console.log(`listening on localhost:${port}/`));

Or, even shorter:

const eon = require('eonjs')(8080);
eon.get('/').text(_ => 'Hello World').listen(port => console.log(`listening on localhost:${port}/`));

Also, Eon has parsing for POST bodies in the form of both urlencoded forms and JSON built-in:

const eon = require('eonjs')(8080);
eon.post('/post').onBody((req, res) => {
    res.end(`You sent me: ${JSON.stringify(req.body)}`);
});
eon.listen(port => console.log(`listening on localhost:${port}/`));

When a POST request is received, the Eon auto-detects whether to use JSON or the standard URL decoder to parse the body.

New: Eon also supports express-like paths:

const eon = require('eonjs')(8080);
eon.get('/user/:name').text(req => `Hello, user ${req.data.name}`);
eon.listen(port => console.log(`listening on localhost:${port}/`));

If you want the full documentation, you can check out this site, or look at the repo: {% github eon-web/eon %}

This post is available on dev