Search This Blog

Thursday, April 14, 2016

Backend of my hobby project => Node.JS

My hobby project is progressing, but before I can really go on, I need to start working a bit on the back-end allowing to store data, for example maps, as well as having a bit of access control.

As you may have saw from my previous posts, I currently decided to use Node.JS simply because it allows me to more easily host my project on an existing Linux VPS I already rent.

Node.JS has many advantages like being able to develop using a single language between the front-end and the back-end, having cool features like socket.io which handles the web sockets for you, and much more.

However Node.JS do have drawbacks (at least in my opinion). All (or most) works using asynchronous call, for example, you connect to a database (async) and make a query (yet another async). You end up easily to have loads of nested anonymous functions just to handle such cases.

This of course makes the code harder to read and to write. But you have also issues about error handling. If an exception is fired, you may simply fail to be able to catch it, which means normally to kill your server, and requires a restart (sure it can be automated). This means whatever you had in memory is lost.

Let me show you an example:
app.post('/backend/OwnerExists'function (req, res, next)
{
    if (!req.body.user)
    {
        res.writeHead(500, { 'Content-Type''text/json' });
        res.write(JSON.stringify({ error: "parameter 'user' is missing." }));
        res.end();
        return;
    }
    var connection = getConnection();
    if (!connection)
    {
        res.writeHead(500, { 'Content-Type''text/json' });
        res.write(JSON.stringify({ error: "connection failed." }));
        res.end();
        return;
    }
    connection.connect(function (err)
    {
        if (err != null)
        {
            connection.end();
            console.log(err);
            res.writeHead(500, { 'Content-Type''text/json' });
            res.write(JSON.stringify({ error: "error with database." }));
            res.end();
            return;
        }
        connection.query('select id from game_owners where name = ?', [req.body.user], function (err1, r1)
        {
            connection.end();
            if (err1 != null)
            {
                console.log(err1);
                res.writeHead(500, { 'Content-Type''text/json' });
                res.write(JSON.stringify({ error: "error with database." }));
                res.end();
                return;
            }
            // Not yet registered            if (r1.length == 0)
            {
                res.writeHead(200, { 'Content-Type''text/json' });
                res.write(JSON.stringify({ result: false }));
                res.end();
                return;
            }
            else            {
                res.writeHead(200, { 'Content-Type''text/json' });
                res.write(JSON.stringify({ result: true }));
                res.end();
                return;
            }
        });
    });
});
This connects to a MySQL database and makes a single query. You see already how many nested function I have.

Sure there is ways to improve the situation using 3rd party libs or working a bit differently, still at the end of the day the same will happen => each connection / query will require a new function callback.

I really wonder how you can handle big projects written with such framework.

No comments:

Post a Comment