Struct
Struct

Reputation: 970

GET and handle data from DB with node.js express

I'm creating a jQuery mobile app and I've a linux server with node.js and express. I think the authentification works fine and the database connection with another db-server, too.

My problem is, that I don't know how to receive and handle the data from my server on client side. Can anybody help me to do that, by giving some code snippet or something else? How can I get and handle data and how can I add a reservation for example?

auth.js on my node.js express server:

/* ************ Authentication ************ */
app.post('/auth', function(req, res) {
    var user = req.body.user;
    var password = req.body.password;
    DoIfAuthenticated(function (){
        res.send(200);
        console.log("Authenticated - Sending 200");
    },req,res)
});

function DoIfAuthenticated(isAuth, req, res){
    Authenticated(isAuth, function(){
        res.send(406);
        console.log("User not authenticated");
    }, req)
}

function Authenticated(isAuth, isNotAuth, req){
    db.serialize(function(){
        var stmt = db.prepare("select password from users where name like ?");
        stmt.get(req.body.user, function(err, row) {
            if(row == undefined){
                isNotAuth();
                console.log("User not exists");
                return;
            }
            if(row.password !== req.body.password){
                isNotAuth();
                console.log("Wrong password");
            }else{
                isAuth();
                console.log("Successful");
            }
        });
    })
}

/* ************************ */
app.get('/getLata', function(req, res){
    DoIfAuthenticated(function() {
        getFromDB("select * from lots where pid = " + req.param("gara"), req, res)
    }, req, res)
})

app.get('/addReservation', function(req, res){
    DoIfAuthenticated(function() {
        getFromDB("insert into reservation (p_lot_id, start_time, end_time)"  +
        "values ("+ req.param("lot") +", TIMESTAMP '" + req.param("start") + "', " +
            "TIMESTAMP '" + req.param("end") + "')", req, res)
    }, req, res)
})

Upvotes: 2

Views: 1782

Answers (1)

Kenan
Kenan

Reputation: 3620

For server side authentication using node and express, you might refer to and reference the popular passport module. See their examples, similar to what you are attempting at https://github.com/jaredhanson/passport-local/tree/master/examples and http://passportjs.org

On the client side, after a user has been authenticated, a user will typically have a session cookie that will be sent with each request. The server side isAuthenticated method would confirm this session is valid, and then process the request. The session cookie is automatically sent by the browser to the server on the same domain.

To get the data after authenticated, you could use jQuery to send an ajax request similar to:

$.get( "getLata", function( data ) {
  $( ".result" ).html( data );
});

To add a reservation, you should strongly consider changing the server side route to accept a POST instead of a GET request, as you are modifying data. A client side ajax post using jQuery would look similar to:

$.post( "addReservation", {lot_id: "123"}, function( data ) {
  $( ".result" ).html( data );
});

Upvotes: 1

Related Questions