Sunday, 9 April 2017

How to efficiently combine Express routing and SQLite

I'm new to Express and SQL, so I don't know the conventional ways of combining the two. Right now I have done it the following way:

app.get('/login', function (req, res) {
    res.render('login');
});

app.get('/home', function (req, res) {
    res.render('home');
});

app.post('/login', function (req, res) {
    db = new sqlite3.Database(file);
    db.serialize(function () {
        [...]
        db.all(query, function (err, rows) {
            if(rows.length == 1) {
                [...]
                res.render('home', {
                    username: rows[0].username
                });
            }
            else {
                res.render('login', {
                    message: "Login not successful!"
                });
            }
        });
    });
    db.close();
});

However, I feel like the routing should be separated from the database stuff. What should I do different? Or is this normal?



via Zarif

No comments:

Post a Comment