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)
})
res.send("some JS object Data")or in any way. Plus, it will not be styled, it will be pure JSON data.