fogo
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

fogo 🔥

Yet another node.js framework, but simpler.

Build Status npm download GitHub issues minified + gzip MIT license npm version GitHub contributors GitHub stars

Install

npm install fogo --save

fogo supports only node 8+ because of async/await.

Why? Why? Why?

fogo, means fire in Portuguese 🤷‍♀️. It is basically a wrapper for the node.js http module.

Sometimes Express, which is a good framework, is too much for your needs or too heavy because of its tons of dependencies, especially if you do microservices or have a lot of CI/CD going on.

Only use fogo if you want to use the http module provided by node.js but without dealing with routes.

fogo does not support middlewares. You really don't need it.

What fogo is good for:

  • microservices
  • restful/json apis

What fogo is not good for:

  • applications using template engines
  • monolithic applications

An alternative to fogo is micro.

Usage

const fogo = require('fogo');
 
const handlers = {
    '/': (req, res, url) => {
        res.writeHead(200); // Pure node.js!
 
        res.end('Hello world!'); // you see?!?
    },
    '/products/:id': {
        get: async (req, res, url, params) => {
            try {
                await validateRequest(req, schema); // Why middlewares? :)
                const product = await getProductFromDB(params.id);
 
                res.writeHead(200);
                res.end(JSON.stringify(product));
            } catch (err) {
                res.writeHead(404);
                res.end('Oops, this product does not exist.');
            }
        }
    },
    '/customers': {
        post: async (req, res) => {
            try {
                await validateRequest(req, schema); // Why middlewares? :)
                const body = await json(req);
                const customer = await createCustomer(body.customer);
 
                res.setHeader('x-foo', 'bar');
                res.writeHead(201);
                res.end('Customer created');
            } catch (err) {
                res.writeHead(403);
                res.end(
                    JSON.stringify({
                        statusCode: 403,
                        message: 'Something crazy just happened!'
                    })
                );
            }
        }
    }
};
 
const notFoundHandler = (req, res) => {
    res.writeHead(404);
    res.end('Not found, sorry!');
};
 
const server = fogo.createServer(handlers, notFoundHandler);
 
// server is just a wrapper for the http.Server class
server.on('clientError', (err, socket) => {
    socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
 
server.listen(3000, () => {
    console.log('Running at localhost:3000!');
});

API

createServer

const server = fogo.createServer(handlers, notFoundListener, errorHandler);

Since fogo.createServer returns a http.Server class, you should be able to use all its methods. Please, check out the documentation for more information.

It creates a server that you can listen to. It accepts two arguments:

handlers: object - required

An object containing your handlers for each route with/without specifying the http method. Specify a function if you want that route to listen to all http methods.

const handlers = {
    '/': (req, res, url, params) => {
        res.end();
    },
    '/welcome': {
        get: (req, res, url, params) => {
            res.end();
        },
        post: (req, res, url, params) => {
            res.end();
        }
    }
};

Arguments received:

notFoundListener: function

If none of you handle has the requested route, this callback is called.

fogo provides a default one.

function defaultNotFound(req, res) {
    res.writeHead(404);
    res.end(http.STATUS_CODES[404]);
}

The response will look like:

Not Found

Arguments received:

errorHandler: function

It's called when an exception is thrown on the request.

fogo provides a default one.

function defaultErrorHandler(req, res, parsedUrl, err) {
    console.log(err);
 
    res.writeHead(500);
    res.end(http.STATUS_CODES[500]);
}

The response will look like:

Internal Server Error

Arguments received:


Made with ❤️ enjoy it!

Package Sidebar

Install

npm i fogo

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

34 kB

Total Files

16

Last publish

Collaborators

  • cezarluiz0