DEV Community

loading...
Cover image for Eon.js - My new little express competitor

Eon.js - My new little express competitor

codemaster138 profile image Jake Sarjeant Originally published at jsblog.netlify.app ・Updated on ・2 min read

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 logo eon-web / eon

A simple framework for creating web services

Eon.js

A simple framework for creating web services

version badge license badge size badge dependency badge downloads badge issues badge closed issues badge pr badge stars badge website badge commit badge size badge repo size badge

Why Eon?

Yes, another web server framework. Whenever a new one comes out, you have to ask yourself: Do I need this? Am I fine to just continue using express/fastify/etc...? I want to present to you the reasons why I began writing this framework and hopefully make your decision a bit easier.

Express is not a small framework

Express is great, because it provides lots of features, but that also makes it a quite large library. With Eon, I tried to reduce the bundle size by writing as much code as possible on my own and adding only the neccessary features.

0 Dependencies

As of v1.11.x, Eon.js has no dependencies whatsoever, thereby further reducing the bundle size and overhead of additional packages to manage.

Installation

To install eon, simply run

npm i eonjs@latest --save

Examples & Usage

Here is an…

Discussion (3)

pic
Editor guide
Collapse
billionaire9 profile image
Anuwong Mongkudkornburee

It's look great! I am looking for small web application for my personal project. THANKS!

Collapse
codemaster138 profile image
Jake Sarjeant Author

Hi! Cool that you like the project!

Collapse
codemaster138 profile image
Jake Sarjeant Author

Also, you should make sure not to use v1.12.x, because I forgot to run my test only to later find it doesn't work at all. Fixed in v1.13.0