DEV Community

Cover image for Simple examples of NodeJS servers: Express, Koa and Hapi | Discussion.
Luiz Calaça
Luiz Calaça

Posted on

Simple examples of NodeJS servers: Express, Koa and Hapi | Discussion.

Hello, Devs!

Keep in your mind:

“The Only Constant in Life Is Change.”- Heraclitus

Currently, we can use Express, tomorrow Koa and after Hapi, maybe not in this sequence, so you really know that is normal many frameworks in our context.

One thing to realize here is the easy way to migrate through theses frameworks, because they have a similar interface to handle routes, callbacks, request and response, start server, listen ports, http methods rest, error handling and others.

Express.js

Express.js’s GitHub repository has 55.8k stars, 205 contributors, 8.3k forks, and 280 releases.

Koa.js

Koa.js’s GitHub repository has 32.2k stars, 276 contributors, and 3.1k forks.

Hapi.js

Hapi.js’s GitHub repository has 13.7k stars, 209 contributors, 9.4k forks.


Look at here each one simple example of Express, Koa and Hapi.

Let's go!

Link: https://expressjs.com/

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
Enter fullscreen mode Exit fullscreen mode

Link: https://koajs.com/

$ npm i koa

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Link: https://hapi.dev/

'use strict';

const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

init();

Enter fullscreen mode Exit fullscreen mode

But what would be a best option?

1 - Your server production has a support for all them? So, it's necessary look at.
2 - The software is dependent of each one of them and has no time and team to migrate?
3 - We have a good community to ask any question if necessary? All of them has an awesome community.
4 - What are the advantages to migrate? We'd have a clean code with some one of them or a better performance?
5 - All are minimalistic, flexible and scalable.

What you think? How to use? You have a case? Share on comments!

Contacts
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca

Top comments (0)